142

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.

3
  • You want to launch an instance of notepad from your program and have it open a specific file?
    – Dismissile
    Oct 29, 2010 at 19:37
  • 1
    Note: At when I tried it with .Net 2.0, Process.Start did not automatically expand "%windir%", though omiting it as in viabhav's answer or expanding it explicitly (Environment.GetEnvironmentVariable("windir")) worked successfully.
    – Brian
    Oct 29, 2010 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: simplygoodcode.com/2012/04/notepad-clone-in-net-winforms.html
    – Luis Perez
    Aug 1, 2012 at 19:06

6 Answers 6

231

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.

5
  • 1
    I would do some escaping if I were you. Oct 29, 2010 at 19:39
  • 1
    I 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, 2010 at 19:43
  • 1
    It's better to pass the file name as a second parameter. Actually, according to the documentation, 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, 2010 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, 2010 at 0:33
  • 15
    Using 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). Jan 29, 2013 at 23:10
33

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.

1
  • And, in addition, it would be good to use a if(File.Exists(fileToOpen)) to avoid running into an exception.
    – Wiccio
    May 29, 2020 at 15:49
26

this will open the file with the default windows program (notepad if you haven't changed it);

Process.Start(@"c:\myfile.txt")
0
16
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
13

You can use Process.Start, calling notepad.exe with the file as a parameter.

 Process.Start(@"notepad.exe", pathToFile);
2
  • 2
    By 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, 2010 at 0:39
  • @Aren: which can be solved calling Environment.ExpandEnvironmentVariables() May 20, 2018 at 17:50
8

Use System.Diagnostics.Process to launch an instance of Notepad.exe.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.