簡體   English   中英

如何從后面的代碼訪問 rstrui.exe?

[英]How to access rstrui.exe from code behind?

請告訴我如何從我的 c# 代碼訪問系統還原“rstrui.exe”。

我嘗試通過調用C:\\Windows\\System32\\rstrui.exe 但它根本無法訪問。

我需要調用這個函數來將我的控件重定向到系統還原。

謝謝....

您可以使用以下屬性訪問 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");

我在 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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM