How I open a file in c#? I don't mean reading it by textreader and readline(). I mean open it as an independent file in notepad.
-
You want to launch an instance of notepad from your program and have it open a specific file? – Dismissile Oct 29 '10 at 19:37
-
1Note: At when I tried it with .Net 2.0, `Process.Start` did not automatically expand "%windir%", though omiting it as in [viabhav's answer](http://stackoverflow.com/questions/4055266/open-a-file-with-notepad-in-c/4055295#4055295) or expanding it explicitly (`Environment.GetEnvironmentVariable("windir")`) worked successfully. – Brian Oct 29 '10 at 20:45
-
You can integrate a notepad clone into your application and customize it to function just the way you want it. I write a notepad clone in C# you can find it here: http://www.simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html – Luis Perez Aug 01 '12 at 19:06
6 Answers
You need System.Diagnostics.Process.Start()
.
The simplest example:
Process.Start("notepad.exe", fileName);
More Generic Approach:
Process.Start(fileName);
The second approach is probably a better practice as this will cause the windows Shell to open up your file with it's associated editor. Additionally, if the file specified does not have an association, it'll use the Open With...
dialog from windows.
Note to those in the comments, thankyou for your input. My quick n' dirty answer was slightly off, i've updated the answer to reflect the correct way.
-
1
-
1I agree that this is one way to do this, another way if you wanted to open the document but not run the program would be to use something along the lines of: richTextBox1.LoadFile(Program.editInC,RichTextBoxStreamType.UnicodePlainText) for loading the actual contents into a file. – Jim Oct 29 '10 at 19:43
-
1It's better to pass the file name as a second parameter. Actually, according to the [documentation](http://msdn.microsoft.com/en-us/library/53ezey2s.aspx), your code shouldn't work, as the single parameter of `Process.Start` is document or application file name, whereas yours is the application name combined with the command line parameter. – Vlad Oct 29 '10 at 20:27
-
@Albin: Thanks, totally overlooked that one :p @Jim: I believe the question was asking how to launch an editor. @Vlad: Thanks, you are correct. – Aren Oct 30 '10 at 00:33
-
15Using `Process.Start(filename)` is a potential command injection, whereby an attacker could substitute `MyTextFile.txt` for `MyMalicious.bat` or `fdisk ...`. Better to use `Process.Start("notepad.exe", filename)`. – Geoff Bennett Jan 29 '13 at 23:10
You are not providing a lot of information, but assuming you want to open just any file on your computer with the application that is specified for the default handler for that filetype, you can use something like this:
var fileToOpen = "SomeFilePathHere";
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = fileToOpen
};
process.Start();
process.WaitForExit();
The UseShellExecute parameter tells Windows to use the default program for the type of file you are opening.
The WaitForExit will cause your application to wait until the application you luanched has been closed.

- 21,178
- 26
- 94
- 142
-
And, in addition, it would be good to use a `if(File.Exists(fileToOpen))` to avoid running into an exception. – Wiccio May 29 '20 at 15:49
this will open the file with the default windows program (notepad if you haven't changed it);
Process.Start(@"c:\myfile.txt")

- 45,724
- 13
- 98
- 148
You can use Process.Start
, calling notepad.exe
with the file as a parameter.
Process.Start(@"notepad.exe", pathToFile);

- 489,969
- 99
- 883
- 1,009
-
2By the way, %pathVariables% do not work with this method. `Process.Start(@"%windir%\notepad.exe");` throws a Win32Exception:"Cannot find file" but normally it should work. – Aren Oct 30 '10 at 00:39
-
@Aren: which can be solved calling Environment.ExpandEnvironmentVariables() – sɐunıɔןɐqɐp May 20 '18 at 17:50