我遇到异常错误,找不到文件
I'm having exception errors, file not found
当我尝试 运行 这个函数时,它在进程启动时一直崩溃。
public static void MapDestinationToSource (string destination, string source)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process ();
// set the file to execute
proc.StartInfo.FileName = "mklink";
proc.StartInfo.Arguments = $"/D \"{source}\" \"{destination}\"";
// Redirect the output stream of the child process.
proc.StartInfo.UseShellExecute = true;
//proc.StartInfo.RedirectStandardOutput = true;
// start the process
proc.Start ();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = proc.StandardOutput.ReadToEnd ();
proc.WaitForExit ();
}
异常:
System.ComponentModel.Win32Exception occurred
HResult=0x80004005
Message=The system cannot find the file specified
Source=System
StackTrace:
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at GameCloud.StorageLibrary.MapDestinationToSource(String destination, String source) in D:\Development\code\sample\server\ClientAgent\ClientAgent\StorageLibrary.cs:line 130
at GameCloud.Program.Main(String[] args) in D:\Development\code\sample\server\ClientAgent\ClientAgent\Program.cs:line 135
当我在命令行上执行命令时,它起作用了。但它不会在代码中出现。我已经设置了安全策略以允许当前用户在没有提升权限的情况下执行 mklink 命令。
如果您正在尝试执行可执行程序 (bob.exe
),请查看下面我删除的答案。
因为你正在尝试 运行 mklink
内置于 cmd
那么你需要使用:
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = $"/C mklink /D \"{source}\" \"{destination}\"";
The docs 状态:
When UseShellExecute is true, the WorkingDirectory property specifies
the location of the executable. If WorkingDirectory is an empty
string, it is assumed that the current directory contains the
executable.
然后:
When UseShellExecute is false, the FileName property can be either a
fully qualified path to the executable, or a simple executable name
that the system will attempt to find within folders specified by the
PATH environment variable.
因此,您需要将 UseShellExecute
设置为 false
(以便您的 PATH
用于查找可执行文件) 或 将 WorkingDirectory
设置为包含可执行文件的文件夹。
当我尝试 运行 这个函数时,它在进程启动时一直崩溃。
public static void MapDestinationToSource (string destination, string source)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process ();
// set the file to execute
proc.StartInfo.FileName = "mklink";
proc.StartInfo.Arguments = $"/D \"{source}\" \"{destination}\"";
// Redirect the output stream of the child process.
proc.StartInfo.UseShellExecute = true;
//proc.StartInfo.RedirectStandardOutput = true;
// start the process
proc.Start ();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = proc.StandardOutput.ReadToEnd ();
proc.WaitForExit ();
}
异常:
System.ComponentModel.Win32Exception occurred
HResult=0x80004005
Message=The system cannot find the file specified
Source=System
StackTrace:
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at GameCloud.StorageLibrary.MapDestinationToSource(String destination, String source) in D:\Development\code\sample\server\ClientAgent\ClientAgent\StorageLibrary.cs:line 130
at GameCloud.Program.Main(String[] args) in D:\Development\code\sample\server\ClientAgent\ClientAgent\Program.cs:line 135
当我在命令行上执行命令时,它起作用了。但它不会在代码中出现。我已经设置了安全策略以允许当前用户在没有提升权限的情况下执行 mklink 命令。
如果您正在尝试执行可执行程序 (bob.exe
),请查看下面我删除的答案。
因为你正在尝试 运行 mklink
内置于 cmd
那么你需要使用:
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = $"/C mklink /D \"{source}\" \"{destination}\"";
The docs 状态:
When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, it is assumed that the current directory contains the executable.
然后:
When UseShellExecute is false, the FileName property can be either a fully qualified path to the executable, or a simple executable name that the system will attempt to find within folders specified by the PATH environment variable.
因此,您需要将 UseShellExecute
设置为 false
(以便您的 PATH
用于查找可执行文件) 或 将 WorkingDirectory
设置为包含可执行文件的文件夹。