Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have the following code in my application --

C#
if (File.Exists("c:\\a.txt"))
{
  try
  {
     Process z = Process.Start("notepad c:\\a.txt");                
  }

  catch(Exception ex)
  {
      MessageBox.Show(ex.Message.ToString());
  }
}



I get a messagebox that states "The system cannot find the file specified"

Can Anyone tell me how to start notepad (to display the file)??
Posted
Updated 2-Apr-20 21:43pm
Comments
[no name] 7-May-12 8:24am    
You need to pass the parameter to notepad with the ProcessStartInfo object.
ZurdoDev 7-May-12 8:37am    
That way will work. The error is referring to Notepad.exe, not to your file. Give a full path to notepad.exe.
nweiher 7-May-12 8:42am    
actually, I have (c:\\windows\\system32\\notepad.exe c:\\a.txt") ...
I'm using Visual Studio 2008 Express Edition ... when I type in ProcessInfo, it states ProcessInfo is undefined -- so I Added Using System.Web and it still can't find ProcessInfo!
Mantu Singh 7-May-12 8:56am    
Sir;
Pls add using System.Diagnostics;

on the top...............

You have the finest control using the method Start(ProcessStartInfo psi)[^], but in your case the method Start(String programFile, String arguments)[^] will suffice. The parameter programFile will be the full path to notepad.exe and the file you want to load will given in the parameter arguments.

Regards,

Manfred
 
Share this answer
 
Comments
Monjurul Habib 8-May-12 16:28pm    
5!
Manfred Rudolf Bihy 8-May-12 17:55pm    
Thank you!
Try:
C#
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("Notepad.Exe", @"D:\Temp\MyLargeTextFile.txt");
p.StartInfo = psi;
p.Start();
 
Share this answer
 
Comments
Manfred Rudolf Bihy 7-May-12 9:23am    
Beaten by one minute, phhhhh! ;)
5+
OriginalGriff 7-May-12 9:39am    
:laugh: 5 anyway!
Monjurul Habib 8-May-12 16:28pm    
5!
Consider using the shell execute command. In this case, the associated application for txt-files will open. In many cases this will be standard windows notepad, but some people, like me ;-), prefer different editors like Notepad++ etc.

C#
Process foo = new Process();
foo.StartInfo.UseShellExecute = true;
foo.StartInfo.FileName = "filename.txt";
foo.Start();
 
Share this answer
 
Use the advice of Mr Wed

Ok use the following code here............


C#
System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo("notepad.exe", "D:\\reg.txt");
System.Diagnostics.Process.Start(pi);



Hope this helps.........
 
Share this answer
 
using System.IO;
using System.Diagnostics;

public void OpenNotePadPlusPlus()
{
string filename = @"C:\Users\Brian\Documents\file I wanna open.txt");
FileInfo fi = new FileInfo(filename);
if (fi.Exists == false) return;
string fname = "\u0022"+filename+"\u0022"; // unicode for double quote marks
string editorpath =
"\u0022"+@"C:\Program Files (x86)\Notepad++\Notepad++.exe"+"\u0022";
Process process;
try
{
process = Process.Start(editorpath,fname);
}
catch (Exception ee)
{
string msg = ee.Message;
// do something with message
return;
}
process.WaitForExit();
// I needed the above wait so i could reprocess stuff after notepad++ edits
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900