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.
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.– JimOct 29, 2010 at 19:43
-
1It'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.Startis document or application file name, whereas yours is the application name combined with the command line parameter.– VladOct 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.– ArenOct 30, 2010 at 0:33
-
15Using
Process.Start(filename)is a potential command injection, whereby an attacker could substituteMyTextFile.txtforMyMalicious.batorfdisk .... Better to useProcess.Start("notepad.exe", filename). Jan 29, 2013 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.
-
And, in addition, it would be good to use a
if(File.Exists(fileToOpen))to avoid running into an exception.– WiccioMay 29, 2020 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")
You can use Process.Start, calling notepad.exe with the file as a parameter.
Process.Start(@"notepad.exe", pathToFile);
-
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.– ArenOct 30, 2010 at 0:39 -
@Aren: which can be solved calling Environment.ExpandEnvironmentVariables() May 20, 2018 at 17:50
Process.Startdid not automatically expand "%windir%", though omiting it as in viabhav's answer or expanding it explicitly (Environment.GetEnvironmentVariable("windir")) worked successfully.