可以';尝试启动进程时找不到指定的文件

本文关键字:找不到 文件 进程 启动 可以 | 更新日期: 2023-09-27 18:27:44

我正在尝试制作一个C#应用程序,它将连接到文件共享,写入文件,然后断开连接。

NetUseCmd = "net use t: ''Hostname'Vol /user:UserName SomePass"
System.Diagnostics.Process.Start(NetUseCmd);
Directory.CreateDirectory(DriveLetter + ":/" + DirName);
StreamWriter file = new StreamWriter(DriveLetter + ":/" + FileName);
file.Write(logdata);
file.Close();
System.Diagnostics.Process.Start("net use " + DriveLetter + ": /del");

在这篇文章的第二行,我看到了错误:

System.ComponentModel.Win32Exception was unhandled
  Message="The system cannot find the file specified"
  Source="System"
  ErrorCode=-2147467259
  NativeErrorCode=2
  StackTrace:
       at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
       at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start(String fileName)
       [...]
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我知道错误消息意味着它找不到net命令,但我不明白它为什么找不到它。

可以';尝试启动进程时找不到指定的文件

您需要传递要用作第一个参数的命令,然后传递任何要作为第二个参数传递给进程的参数。因此:

System.Diagnostics.Process.Start( "net", "use t: ''Hostname'Vol /user:UserName SomePass");

有关更多详细信息,请参阅文档。

如果要将某些参数传递给net.exe,则应使用Process.Start():的另一个重载版本

string arguments = @"use t: ''Hostname'Vol /user:UserName SomePass";
System.Diagnostics.Process.Start("net", arguments);

请检查您的源代码是否也有双反斜杠(如果没有@,这是一个转义序列)。