question

Andreasss-5315 avatar image
0 Votes"
Andreasss-5315 asked NguyenTuanThanhGAMAIS-5214 published

How to execute this command in cmd prompt using C#

Hello!

I am trying to execute a command in cmd.exe using C#.

Normally, I would open the cmd.exe prompt manually and I would go the the directory: "C:\myproject" which is the directory I need to first select.

Secondly, I would manually run the command: "node fileWithCommands.js" which is a ".js" file which exists in the "C:\myproject" directory. I would then VISUALLY see commands executing in the actual cmd window that is opened.

So now I am trying to emulate this above manual task by executing that using C#. But the cmd window is only opening with "C:\myproject" selected and the below command is not executing?

Thank you!

Code not executing?

 p.StartInfo.Arguments = "node fileWithCommands.js";

Complete code:

             Process p = new Process();
             p.StartInfo.FileName = "C:\\Windows\\system32\\cmd.exe";
             p.StartInfo.WorkingDirectory = @"C:\myproject";
             p.StartInfo.Arguments = "node fileWithCommands.js";
             p.Start();












dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered JaneS-8455 published

@Andreasss-5315

Try the following

 var p = new Process
 {
     StartInfo =
     {
         FileName = "node", 
         WorkingDirectory = @"C:\myproject", 
         Arguments = "fileWithCommands.js"
     }
 };
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, I tried the code. This seems to work fine now.
Thank you for your help!

             var p = new Process
             {
                 StartInfo =
                 {
                     FileName = "node",                        
                     WorkingDirectory = @"C:\myproject",
                     Arguments = "fileWithCommands.js"
                 }
             }.Start();




0 Votes 0 ·

thanks a lot this was very helpful!

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered NguyenTuanThanhGAMAIS-5214 published

Try with

 p.StartInfo.Arguments = "/C node fileWithCommands.js";
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The code is not executed using this command and the cmd window is not shown either I am afraid.

0 Votes 0 ·

thanks you. I try and run success full with python .py file

0 Votes 0 ·