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?

2

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

7 Comments

I still get the same error. It is in a winforms application that I am trying to run the .cmd file
@ZankhanaRana could you please confirm that the path for the cmd file you are running is correct?
@ZankhanaRana also please try adding manifest file (ex: codeproject.com/Questions/629067/…) like this: <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> or edit your existing manifest.
I checked and confirmed that the path is correct. And I added a manifest file with the line in the previous comment. I still get the same error.
@ZankhanaRana I've updated the post. Would you please try the updated version?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.