dj0965
dj0965

Reputation: 135

How to open notepad with cmd?

I want to open notepad with CMD, using C# but the path has a space in it. I know that there are a lot of questions similar to this, but I couldn't get any of those solutions to work with my example. I do not know why. If anyone wants to help, it would be greatly appreciated.

  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 START ""C:\Users\Dale\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\notepad.exe""";
  process.StartInfo = startInfo;
  process.Start();

There is no error message, but nothing happens in the command prompt, and notepad doesn't open. Another issue is that the command prompt is visible even though I added

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

Upvotes: 1

Views: 31141

Answers (4)

Karthik Nishanth
Karthik Nishanth

Reputation: 2020

The safe and better approach is to go with

string notepad_path = System.Environment.SystemDirectory + "\notepad.exe";

Upvotes: 0

BartoszKP
BartoszKP

Reputation: 35921

You surely don't have the notepad application in your Start Menu, there's only a shortcut there. Usually notepad is located here:

C:\Windows\System32\notepad.exe

What might be misleading, is that clicking "Open file location" on the notepad icon in Start Menu takes you to the place when the shortcut is placed. However, you may notice that it's only a shortcut because of the little arrow icon in the corner. Then, you can right click and choose "Open file location" again - it will point you to the right place this time.

Upvotes: 8

Sense
Sense

Reputation: 181

I assume the path and all that is correct.

In C#, adding a @ before a string takes care of special characters that otherwise would need an escape symbol in front of it (\).

@"/C START ""C:\Users\Dale\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\notepad.exe"""

This should expand to /C START ""C:\Users\Dale\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\notepad.exe"". I guess it has some problems finding that path. Maybe reducing the double quotes to one on each side will help.

Upvotes: -1

DYoung
DYoung

Reputation: 114

C# is probably ignoring the double double quotes ie, the "".

Try escaping the quotes with backslash, ie:

startInfo.Arguments = @"/C START "\"C:\Users\Dale\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\notepad.exe\""";

Upvotes: -1

Related Questions