1

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?

Zankhana Rana
  • 169
  • 2
  • 16
  • Please read this [System.ComponentModel.Win32Exception: Access is denied](https://stackoverflow.com/questions/14194146/system-componentmodel-win32exception-access-is-denied-error) and this [System.ComponentModel.Win32Exception: Access is denied](https://support.microsoft.com/de-at/help/867466/access-is-denied-error-message-when-you-run-a-batch-job-on-a-windows-s) – Rans Apr 17 '20 at 18:12
  • I checked the folder and the cmd.exe properties. The permissions look okay. I also tried running the code above in administrator mode. I still get the same error. – Zankhana Rana Apr 17 '20 at 18:19

1 Answers1

1

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);
Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42