2

So basically, I Want to execute a command in cmd because i need to open Paint from it, Code i tried:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C mspaint.exe, openFileDialog1.FileName;"
process.StartInfo = startInfo;
process.Start();

Can someone help me? I Want to execute paint with a png file choose by user.

Thanks Trevor for helping, Maybe the other code will work.

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C mspaint.exe, " + openFileDialog1.FileName;
            process.StartInfo = startInfo;
            process.Start();

Code working fine

  • Does this answer your question? [Run Command Prompt Commands](https://stackoverflow.com/questions/1469764/run-command-prompt-commands) – ARC Mar 23 '22 at 19:21
  • 2
    I'm going to bet `startInfo.Arguments = "/C mspaint.exe, openFileDialog1.FileName;"` is your issue. Where does `openFileDialog1.FileName` come from? Anyways something like `startInfo.Arguments = $"/C mspaint.exe, {openFileDialog1.FileName}";` should work or `startInfo.Arguments = "/C mspaint.exe, " + openFileDialog1.FileName;` – Trevor Mar 23 '22 at 19:30
  • Thanks, And yes, It is the issue. I Will try it. – PurpleVibe32 Mar 25 '22 at 22:15

2 Answers2

2

To open a specific file using mspaint you can specify the file path as an argument (without any comma):

mspaint <filePath>

From the code you can do the following:

Process proc = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = $"/C mspaint.exe {openFileDialog1.FileName}",
        WindowStyle = ProcessWindowStyle.Hidden
    }
};

proc.Start();
Ruslan Gilmutdinov
  • 1,097
  • 2
  • 8
  • 19
0

From Trevor's comment:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C mspaint.exe, " + openFileDialog1.FileName;
process.StartInfo = startInfo;
process.Start();
Jeremy Caney
  • 6,191
  • 35
  • 44
  • 70