Program w c# wykonujący komendę sfc /scannow

0

Część. Potrzebuje napisać program *.exe w c#, który będzie pozwalał na wykonanie sfc /scannow . Poniżej wysyłam kod, który kompiluje i wykonuje jako administrator, ale niestety wyskakuje błąd: Windows Resource Protection could not start the repair service.

public static void run()
        {
            Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "cmd.exe";
            info.RedirectStandardInput = true;
            info.UseShellExecute = false;
            p.StartInfo = info;
            p.Start();
            using (StreamWriter sw = p.StandardInput)
            {
                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine("echo on");
                    sw.WriteLine("sfc /scannow");
                    Console.ReadKey();
                }
            }

Prosiłbym o pomoc

2

Zobacz ten wątek: https://stackoverflow.com/questions/20871514/how-to-launch-sfc-programatically-on-windows-vista-7-8

So I guess the solution to your problem will involve either switching your main program to 64 bit (probably a little drastic), or splicing a 64 bit process into the chain so that the 64 bit sfc can be run.

1

ale właściwie po co odpalasz cmd.exe i piszesz mu w okno, zamiast odpalić po prostu sfc.exe z odpowiednim parametrem?

0

już tak zrobiłem. jest Ok. zwraca stringa, ale niestety w oknie konsoli nic sie nie wyświetla, a chciałbym widzieć postęp

0

Jeszcze drugie pytanie. Ten sam kod tylko że w aplikacji konsolowej działa.

public static void run()
        {
            string snat = Environment.GetEnvironmentVariable("windir") + @"\sysnative\sfc.exe";
            Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.Arguments = "/scannow";
            info.FileName = snat;
            info.RedirectStandardInput = true;
            info.UseShellExecute = false;
            p.StartInfo = info;
            p.Start();
        }

Czy idzie zagnieździć ten program konsolowy (plik exe) w programie okienkowym? Tak żeby był to jeden plik (po skompilowaniu wersji okienkowej).

1
Luu888 napisał(a):
public static void run()
        {
            string snat = Environment.GetEnvironmentVariable("windir") + @"\sysnative\sfc.exe";

U mnie na 32-bitowym Win10 nie ma żadnego "sysnative", więc jeśli to ma z założenia działać na 32- i na 64-bitowym systemie, to fail.

0

Udało mi się to ogarnąć. Zagnieździłem plik exe (podprogram) w głównym programie .

Kod podprogramu:

static void Main(string[] args)
        {
                 run();
                 delete();
        }
public static void run()
        {
            string snat = Environment.GetEnvironmentVariable("windir") + @"\sysnative\sfc.exe";
            Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.Arguments = "/scannow";
            info.FileName = snat;
            info.RedirectStandardInput = true;
            info.UseShellExecute = false;
            p.StartInfo = info;
            p.Start();
        }
public static void delete()
            {
                Process cmd = new Process();
                cmd.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe";
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.UseShellExecute = false;
                cmd.Start();
                cmd.StandardInput.WriteLine("del skryptsfc.exe");
                cmd.StandardInput.Flush();
                cmd.StandardInput.Close();
                cmd.WaitForExit();
                Console.WriteLine(cmd.StandardOutput.ReadToEnd());
                Console.ReadKey();
        }

kod programu:

public void dismsfc()
            {
                byte[] exeBytes = Properties.Resources.skryptsfc;
                string exeToRun = @"skryptsfc.exe";
                if (File.Exists(exeToRun))
                    {
                        Process.Start(exeToRun);
                    }
                else
                    {
                        using (FileStream exeFile = new FileStream(exeToRun, FileMode.CreateNew))
                        {
                            exeFile.Write(exeBytes, 0, exeBytes.Length);
                        }

                        using (Process exeProcess = Process.Start(exeToRun))
                        {
                            exeProcess.WaitForExit();
                        }
                    }
            }

1 użytkowników online, w tym zalogowanych: 0, gości: 1