110,579
社区成员
发帖
与我相关
我的任务
分享
private static string connectComp(string command)
{
Process p;
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
p.StandardInput.WriteLine("exit");
p.WaitForExit();
string s = p.StandardOutput.ReadToEnd();
p.Close();
return s;
}
public static bool TestConnectSrc(string ipaddress1,string username,string password,out string msg)
{
msg = "";
bool isok;
string s = connectComp(string.Format(@"net use \\{0} {1} /user:{2}", new object[] { ipaddress1, password, username}));
if (s.Contains("successfully"))
{
msg += "文件連接成功\r\n";
isok =true;
}
else
{
msg += "文件連接失敗\r\n";
isok =false;
}
connectComp(string.Format(@"net use \\{0} /delete",ipaddress1));
return isok;
}
private static string connectComp(string command)
{
Process p;
p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(command);
//p.StandardInput.WriteLine(@"cls");
p.StandardInput.WriteLine(@"%errorlevel%"/* or: echo %errorlevel% */); //分别尝试两种方式..
p.StandardInput.WriteLine("exit");
p.WaitForExit();
string s = p.StandardOutput.ReadToEnd();
p.Close();
return s;
}
public static bool TestConnectSrc(string ipaddress1,string username,string password,out string msg)
{
msg = "";
bool isok;
string s = connectComp(string.Format(@"net use \\{0} {1} /user:{2}", new object[] { ipaddress1, password, username}));
if (s.Contains("0")/* or: s == "0" */)
{
msg += "文件連接成功\r\n"; isok =true;
}
else
{
msg += "文件連接失敗\r\n"; isok =false;
}
connectComp(string.Format(@"net use \\{0} /delete",ipaddress1));
return isok;
}
connectComp(string.Format(@"net use \\{0} {1} /user:{2}", new object[] { ipaddress1, password, username}));
if (connectComp(@"%errorlevel%"/* or: echo %errorlevel% */) == "0") //分别尝试两种方式..
{
msg += "文件連接成功\r\n"; isok = true;
}
else
{
msg += "文件連接失敗\r\n"; isok = false;
}
connectComp(string.Format(@"net use \\{0} /delete",ipaddress1));