C#如何访问共享文件夹或者磁盘
1个回答
展开全部
public static bool Connect(string remoteHost, 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 " + remoteHost + " " + passWord + " /user:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
WriteLog.WirteDayLog("msg:" + errormsg);
proc.StandardError.Close();
if (String.IsNullOrEmpty(errormsg))
{
Flag = true;
}
}
catch (Exception ex)
{
WriteLog.WirteDayLog(ex.ToString());
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
Connect(@"\\192.168.15.90", "administrator", "123")
{
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 " + remoteHost + " " + passWord + " /user:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
WriteLog.WirteDayLog("msg:" + errormsg);
proc.StandardError.Close();
if (String.IsNullOrEmpty(errormsg))
{
Flag = true;
}
}
catch (Exception ex)
{
WriteLog.WirteDayLog(ex.ToString());
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
Connect(@"\\192.168.15.90", "administrator", "123")
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询