2

I have C# program which finds the Drive Letter of the inserted USB and assign it to a variable _usbDriveLetter.

I need to pass the value of _usbDriveLetter to a batch file which further executes the CHKDSK _usbDriveLetter command.

I have the following code to pass the value of _usbDriveLetter and execute the batch file.

        string _usbDriveLetter = @"F:\"; //For testing here
        string MyBatchFile = @"<FILEPATH>\chkdsk.cmd";

        ProcessStartInfo proc = new ProcessStartInfo();
        proc.CreateNoWindow = true;
        proc.FileName = MyBatchFile;
        proc.Arguments = usbDriveLetter ;
        Process.Start(proc);

Question: As soon as the code reach the line to execute the batch, the following window appear to allow the CMD.exe to start. How can I make the execution in background/Silent (at least without any user interaction).

enter image description here

skm
  • 5,015
  • 8
  • 43
  • 104
  • 2
    Either disable UAC or start the main program with high elevated rights – dcg Feb 28 '17 at 13:47
  • I am already elevating the user rights inside the .cmd file. Running my application with elevated rights is not possible. – skm Feb 28 '17 at 13:48
  • 3
    If UAC is switched on it requires user interaction, that's the whole point. You will always get a prompt like this. – Equalsk Feb 28 '17 at 13:53
  • http://stackoverflow.com/questions/739101/launching-process-in-c-sharp-without-distracting-console-window – lordkain Feb 28 '17 at 14:37
  • @lordkain: Problem is not the console window. My problem is the Popup window where you have to reply in Yes/No as shown in the screenshot. – skm Feb 28 '17 at 14:56

2 Answers2

1

This should work.

Process runprogram = new Process();
                            ProcessStartInfo programinfo = new ProcessStartInfo();
                            programinfo.WindowStyle = ProcessWindowStyle.Hidden;
                            programinfo.CreateNoWindow = true;
                            programinfo.UseShellExecute = false;
                            programinfo.RedirectStandardOutput = true;
                            programinfo.FileName = "cmd.exe";
                            programinfo.Arguments = " /C \"<FILEPATH>\chkdsk.cmd\"";
                            runprogram.StartInfo = programinfo;
                            runprogram.Start();

Also ask for privilege elevation when first running the program.

  • thanks, but it won't solve the problem since my problem is not the Console window. My problem is the windows popup message. Apparently, there is no solution to this problem except running my .exe with elevated rights or changing UAC settings. – skm Feb 28 '17 at 14:58
  • @skm I just tested it and it does work? in your application add a manifest file with the execution level set to highestAvailable and use that code. – Ricky Divjakovski Feb 28 '17 at 15:10
  • could you provide a sample Manifest for this purpose? – skm Feb 28 '17 at 15:16
  • Heres the manifest - http://pastebin.com/7B6TWEga As the invoker has elevated privileges whatever the invoker calls will not request higher privilege access as its being executed at a higher level already. – Ricky Divjakovski Feb 28 '17 at 15:18
  • OP already said the caller is not elevated and that running his application with elevation is not possible. – Equalsk Feb 28 '17 at 15:24
  • Theres absolutely no way of avoiding it unless his caller is elevated. – Ricky Divjakovski Feb 28 '17 at 15:30
0

You could try converting the .cmd to a .exe and setting the property to invisible then run the EXE from C#

Here's the resource to do that - http://www.f2ko.de/en/ob2e.php

Pandemonium1x
  • 81
  • 1
  • 1
  • 5