Search code examples
c#visual-studiocmdprocesswindows-defender

System.ComponentModel.Win32Exception c#


This is my code:

    string damnfile = System.IO.Path.GetDirectoryName(choofdlog.FileName);
    ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Maximized;
    startInfo.Arguments = "-Scan -ScanType 3 -File" + damnfile;
    Process p = new Process();
    p.StartInfo = startInfo;
    p.Start();

As you can see, I want to run WinDefender scan for a specific file, but it throws error that the process is not found, but this code works:

        ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.Arguments = "-a";
        Process p = new Process();
        p.StartInfo = startInfo;
        p.Start();

Also I want in the second code to redirect the cmd output to listBox, but thats the second problem, first help me with the defender scan please.


Solution

  • Solveded like now its opening, because I replaced

     ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
    

    with

    ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\\Windows Defender\\MpCmdRun.exe");
    

    Now appeared another problem: even I passed some arguments, the windows immediately close.