-1

I want to execute this command in CMD using C#.

rstrui.exe

This works normally if I manually open CMD and run this command. It should start a system process called System restore. But it doesn't open the process if I do it using C#. Here is my method for running the commands in CMD.

 public static string ExecuteCommand(string command)
    {

        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command)
        {
            CreateNoWindow = false
        };

        using (Process proc = new Process())
        {
            proc.StartInfo = procStartInfo;
            proc.Start();

            string output = proc.StandardOutput.ReadToEnd();

            if (string.IsNullOrEmpty(output))
                output = proc.StandardError.ReadToEnd();

            return output;
        }

    }

This is the code for running the command.

 ExecuteCommand("rstrui.exe");

Pls help me to run this command using C#. Thanks in advance.

KrakenZ
  • 25
  • 7
  • The C# [Process Class](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process) is a C# wrapper class for the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) called by any Windows executable capable running another executable like `cmd.exe` or `explorer.exe` or `powershell.exe`, etc. without or with using the [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure. – Mofi Oct 09 '22 at 13:45
  • I recommend to read the three referenced Microsoft documentation pages carefully from top to bottom to get real knowledge about running an executable from within a program. There is absolutely no need to start with C# `Process` (`CreateProces`) first `cmd.exe` with argument `/c` (execute command line and close) just for letting `cmd.exe` call `CreateProcess` to run `%SystemRoot%\System32\rstrui.exe`. There can be run with C# `Process` directly the executable `rstrui.exe` with its full path without making the task more complicated by running first `cmd.exe` to run `rstrui.exe`. – Mofi Oct 09 '22 at 13:48
  • [Environment.SpecialFolder Enum](https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder) should be used to get the path of system directory of Windows which contains `rstrui.exe`. But there must be something very important taken into account - the Windows [File System Redirector](https://learn.microsoft.com/en-us/windows/win32/winprog64/file-system-redirector) because on 64-bit Windows the executable `rstrui.exe` exists only as 64-bit executable in `%SystemRoot%\System32` and not as 32-bit executable in `%SystemRoot%\SysWOW64`. – Mofi Oct 09 '22 at 14:00
  • 1
    The C# project written is most likely configured as 32-bit x86 application and therefore 32-bit `%SystemRoot%\SysWOW64\cmd.exe` was executed which searched with the local environment variables `PATH` and `PATHEXT` for `rstrui.exe` and could not find it because of the file system redirector which redirected all accesses of directory `%SystemRoot%\System32` to `%SystemRoot%\SysWOW64`. The referenced Microsoft documentation for the file system redirector explains how a C++/C# coded application can turn off temporarily the file system redirector for running a 64-bit executable from an x86 program. – Mofi Oct 09 '22 at 14:06
  • 1
    [How can disable redirection on win64?](https://stackoverflow.com/questions/17487653/how-can-disable-redirection-on-win64) or [Disabling WOW64 File System Redirection from C#](https://www.tonycamilli.com/blog/2005/07/disabling-wow64-file-system). – Mofi Oct 10 '22 at 06:34

1 Answers1

0

The problem was that I had to disable redirection. First make a class as in this code:

 public class Wow64Interop
{
    [DllImport("Kernel32.Dll", EntryPoint = "Wow64EnableWow64FsRedirection")]
    public static extern bool EnableWow64FSRedirection(bool enable);
}

Then add this code just above where you want to disable it.

 Wow64Interop.EnableWow64FSRedirection(false);
KrakenZ
  • 25
  • 7