skm
skm

Reputation: 5679

Execute .cmd file from C# without any popup window

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

Upvotes: 2

Views: 2391

Answers (2)

Ricky Divjakovski
Ricky Divjakovski

Reputation: 428

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.

Upvotes: 1

Pandemonium1x
Pandemonium1x

Reputation: 81

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

Upvotes: 0

Related Questions