How to access rstrui.exe from code behind?

787 views Asked by At

Please tell me how could i access system restore "rstrui.exe" from my c# code.

I tried by calling C:\Windows\System32\rstrui.exe; But it is not accessible at all.

I need to call this function for redirecting my controls to System Restore.

thanks....

2

There are 2 answers

9
Sheridan On

You can access C:\Windows\System32 using the following property:

Environment.SystemDirectory

Environment.SystemDirectory Property

You can run the executable using the following method:

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

Path.Combine Method

Process.Start Method

UPDATE >>>

Ahhh... now I see your problem.

When accessing the System32 folder from 32 bit code on 64bit Windows 7 and Vista (maybe Windows 8 too), Windows 'cleverly' changes that part of the request path to SysWow64. That is why you may have got a 'could not find path' error. In order to get around this, you can use the following:

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

A more complete answer might be:

if (Environment.Is64BitProcess)
{
    Process.Start(Path.Combine(Environment.SystemDirectory, "rstrui.exe"));
}
else Process.Start("C:\\Windows\\sysnative\\rstrui.exe");
0
Dude4 On

I m running it all on a 64bit system, but still nothing works. So I managed to resolve this:

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

To enable it:

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