Zankhana Rana
Zankhana Rana

Reputation: 169

How to execute .cmd file using C#?

I have a command(.cmd) file with few steps in it. I want to execute this file using a C# code

This is the code I wrote-

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Users\zrana\Desktop\"
startInfo.FileName = @"C:\Users\zrana\Desktop\test.cmd"
process.StartInfo = startInfo;
process.Start();

When I run this program, I get an exception System.ComponentModel.Win32Exception: 'Access is denied'. Is this the right way to do this?

Upvotes: 1

Views: 799

Answers (1)

Arsen Khachaturyan
Arsen Khachaturyan

Reputation: 8330

You are getting error because your executable doesn't run in Adminstrator mode.
Try to build your project and then from the build output folder right click on the exe and choose "Run as Administrator" if that doesn't help let me know, please.


Update

Please try running your cmd file by calling it from the Windows cmd.exe like this:

var testCmd = @"/C C:\Users\zrana\Desktop\test.cmd";
System.Diagnostics.Process.Start("cmd.exe", testCmd);

Upvotes: 1

Related Questions