-1

请告诉我如何从我的 c# 代码访问系统还原“rstrui.exe”。

我试过打电话C:\Windows\System32\rstrui.exe;但它根本无法访问。

我需要调用这个函数来将我的控件重定向到系统还原。

谢谢....

4

2 回答 2

0

您可以使用以下属性访问 C:\Windows\System32:

Environment.SystemDirectory

Environment.SystemDirectory 属性

您可以使用以下方法运行可执行文件:

Process.Start(Path.Combine(Environment.SystemDirectory, "rstrui.exe"));

路径组合方法

Process.Start 方法

更新>>>

啊……现在我看到了你的问题。

在 64 位 Windows 7 和 Vista(也可能是 Windows 8)上从 32 位代码访问System32文件夹时,Windows 会“巧妙地”将请求路径的那部分更改为SysWow64. 这就是您可能遇到“找不到路径”错误的原因。为了解决这个问题,您可以使用以下方法:

Process.Start(@"C:\Windows\SysNative\rstrui.exe");

更完整的答案可能是:

if (Environment.Is64BitProcess)
{
    Process.Start(Path.Combine(Environment.SystemDirectory, "rstrui.exe"));
}
else Process.Start("C:\\Windows\\sysnative\\rstrui.exe");
于 2013-07-22T12:33:42.597 回答
0

我在 64 位系统上运行它,但仍然没有任何效果。所以我设法解决了这个问题:

IntPtr wow64Value = IntPtr.Zero;
        try
        {
            Wow64Interop.DisableWow64FSRedirection(ref wow64Value);
            ProcessStartInfo psi1 =
        new ProcessStartInfo("cmd.exe");

            psi1.UseShellExecute = false;
            psi1.RedirectStandardOutput = true;
            psi1.RedirectStandardInput = true;
            psi1.CreateNoWindow = true;
            psi1.Verb = "runas";


            Process ps1 = Process.Start(psi1);
            ps1.EnableRaisingEvents = true;
            StreamWriter inputWrite1 = ps1.StandardInput;

            // uses extra cheap logging facility
            inputWrite1.WriteLine("chcp 437");
            inputWrite1.WriteLine("rstrui.exe");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unabled to disable/enable WOW64 File System Redirection");
            Console.WriteLine(ex.Message);
        }
        finally
        {
            // 3. Let the Wow64FSRedirection with its initially state
            Wow64Interop.Wow64RevertWow64FsRedirection(wow64Value);
        }

要启用它:

public class Wow64Interop
    {
        const string Kernel32dll = "Kernel32.Dll";

        [DllImport(Kernel32dll, EntryPoint = "Wow64DisableWow64FsRedirection")]
        public static extern bool DisableWow64FSRedirection(ref IntPtr ptr);

        [DllImport(Kernel32dll, EntryPoint = "Wow64RevertWow64FsRedirection")]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
    }
于 2019-12-12T14:10:10.143 回答