文件存在但在运行时找不到c#

时间:2013-12-09 08:34:56

标签: c# file-io

你好我的代码是这样的:

if (result == System.Windows.Forms.DialogResult.Yes)
{
    try
    {
         Process myProcess = new Process();
         // Launch the usb backup creator
         myProcess.StartInfo.UseShellExecute = false;
         // get path of recovery drive
         myProcess.StartInfo.FileName = (Environment.SystemDirectory + "\\RecoveryDrive.exe"); ;
         myProcess.StartInfo.CreateNoWindow = true;
         myProcess.Start();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message,ex.Source);
     }
}

该文件存在于system32目录中,但抛出的异常表明该文件不存在?
这与UAC权限有关吗?

1 个答案:

答案 0 :(得分:2)

如果您遇到file system redirector的影响,因为您正在以32位进程运行(针对32位编译或针对具有“首选32位”的AnyCPU),那么您可能希望使用以下代码:

Path.Combine(Environment.GetFolderPath(SpecialFolder.Windows),
             "sysnative",
             "RecoveryDrive.exe");

获取正确的路径。您可以通过选中Environment.Is64BitProcess属性确定是否是这种情况。

参考文献: