zoukankan      html  css  js  c++  java
  • C# 批量登陆远程目录

    服务器很无语,每次重启,之前登陆的凭证就没了,每次都要手动输入太麻烦。

    写了这个程序进行批量登陆。

    代码:

     public string connectState(string path/*要访问的文件路径*/, string userName, string passWord)
            {
                bool Flag = false;
                Process proc = new Process();
                try
                {
                    proc.StartInfo.FileName = "cmd.exe";
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.RedirectStandardInput = true;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.CreateNoWindow = true;
                    proc.Start();
                    //登录验证
                    string dosLine = @"net use \" + path + " " + passWord + " /user:" + userName;
                    //string dosLine = @"net use \" + path + " " + passWord + " /User:domain\" + userName;
                    //proc.StandardInput.WriteLine(@"net use \" + path + " /del /y");
                    //proc.StandardInput.WriteLine("net use * /del /y");
                    proc.StandardInput.WriteLine(dosLine);
                    proc.StandardInput.WriteLine("exit");
                    while (!proc.HasExited)
                    {
                        proc.WaitForExit(1000);
                    }
                    string errormsg = proc.StandardError.ReadToEnd();
                    proc.StandardError.Close();
                    if (string.IsNullOrEmpty(errormsg))
                    {
                        Flag = true;
                    }
                    else
                    {
                        throw new Exception(errormsg);
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message;
                    throw ex;
                }
                finally
                {
                    proc.Close();
                    proc.Dispose();
                }
                return Flag.ToString();
            }
    

      多个路径的话,自己写个循环就好了。

    最后补充一句就是之前遇到很奇葩的问题导致登陆不了,最后的解决方式是:每次登陆之前都把电脑上已经存在的凭据清除。就好了。。句子是:net use * /del /y

    为API生,为框架死,为debug奋斗一辈子;吃符号亏,上大小写的当,最后死在需求上。
  • 相关阅读:
    “非工作总结”之快门—我的镜头见过你
    书摘:日本式管理和依靠自己
    寒冬日,找阳光
    模式自由(Schemafree)和数据存储的非格式化趋势
    心体澄澈,意气和平
    思考些管理的事情
    含沙射影,业镜照胆
    临崖之马,上滩之舟—凡事一定要区别不同情况对待
    [转]HttpContext.Current.Cache 和 HttpRuntime.Cache
    句柄、引用、指针与对象(转)
  • 原文地址:https://www.cnblogs.com/ChaunceyWan/p/10057971.html
Copyright © 2011-2022 走看看