在C# Windows服务程序里,怎么用shell 执行net use命令。
SST_X 2020-04-13 10:41:56 VB.NET写的可以。C#该怎么用?
Shared Function checkPathExists(ByVal PathStr As String) As Boolean
Dim s As String = "cmd.exe /c net use " + NetUsePath + " " + Chr(34) + PWD + Chr(34) + " /user:" + Chr(34) + userName + Chr(34)
My.Computer.FileSystem.WriteAllText(My.Application.Info.DirectoryPath & "\logfile" & "\ttt.txt", Strings.Format(Now, "yyyy/MM/dd HH:mm:ss") & " " & s, True, System.Text.Encoding.GetEncoding("GB2312"))
Shell(s, 1)
Thread.Sleep(500)
If System.IO.Directory.Exists(PathStr) = False Then
checkPathExists = False
Else
checkPathExists = True
End If
End Function
C#代码,执行无效,一直返回false
private bool NetUse()
{
Process process = new Process();
//设定程序名
process.StartInfo.FileName = "cmd.exe";
//关闭Shell的使用
process.StartInfo.UseShellExecute = false;
////重新定向标准输入,输入,错误输出
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
//设置cmd窗口不显示
process.StartInfo.CreateNoWindow = false;
//开始
process.Start();
//输入命令,退出
string cmd = @"net use " + ClassConfig.netPath + " " + ClassConfig.pwd + " /user:admin";
process.StandardInput.WriteLine(cmd);
//net use \\plant1-test\shared "plant1" /user:"asia\user"
//net use \\192.168.0.4 admin1234 /user:admin
process.StandardInput.WriteLine("exit");
System.Threading.Thread.Sleep(1000);
if (Directory.Exists(ClassConfig.netPath) == false)
{
System.IO.File.AppendAllText(Application.StartupPath + "\\ErrNETUSE.txt", Directory.Exists(ClassConfig.netPath) + "\n");
return false;
}
else
{
System.IO.File.AppendAllText(Application.StartupPath + "\\ErrNETUSE.txt", Directory.Exists(ClassConfig.netPath) + "\n");
return true;
}
}