0

The code, in both case is identical:

This is working and opening the text file in notepad

editor = "notepad.exe";
if (File.Exists(briefingFile))
{
  Process.Start(editor, briefingFile);
}

This one does is not work:

editor = "notepad++.exe";
if (File.Exists(briefingFile))
{
  Process.Start(editor, briefingFile);
}

It is the same test file and I have notepad++ installed. I Also tried to specify notepad++ with full path but the result is the same. Instead of opening notepad++ I get the attached error messages which tries to create new file or open missing files.

1st error

which i click NO i get this

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
dandan21
  • 37
  • 1
  • 7
  • There's no context for the first error message and the second one says the file doesn't exist, which is all that can be said without guessing. You need to include more information. The only thing you've given is the value of `editor` which I'm inclined to believe isn't what it was set to when you got those errors. – Jesse Feb 23 '22 at 19:10
  • 1
    `briefing.txt` should be changed to full path - it can't find it otherwise. – Poul Bak Feb 23 '22 at 19:13
  • 3
    Because the normal notepad.exe is registered in the Windows Environment path variables. And notepad++ is not. – Charles Feb 23 '22 at 19:14
  • briefing.txt exists. I am running the same code while only the "editor" is changing. "briefing.txt" opens fine with notepad. but with I try to open the SAME file with notepad++ it starts teh errors. Can this be due to the ++ signs? – dandan21 Feb 23 '22 at 19:16
  • I think another problem might be that you try to open a file in a `user folder`. And depending on the access rights notepad++ cannot read there. – Charles Feb 23 '22 at 20:19

4 Answers4

1

The notepad.exe file is part of Windows and lives in the Windows folder, which is part of the default search path (an environment variable). Notepad++.exe is not part of Windows, and so its home folder is not part of the default search path.

Therefore, to open a process using Notepad++ you must also know the full path to the program.

When trying the full path, make sure you escape the folder separator characters properly, and you must make sure to account for spaces in your path. In this case, the reason you see the C:\Program error is because you haven't yet accounted for the space in Program Files.

editor = @"""C:\Program Files\Notepad++\notepad++.exe""";
try 
{
  Process.Start(editor, briefingFile);
}
catch(Exception ex)
{
   // Do something here
}

Also note how I switched to an exception handler instead of File.Exists(). Disk I/O is one of those rare places where you should prefer handling the exception. File.Exists() is particularly bad for this, and should be avoided.


One other option here is if you have enough control for your target machines to know for sure Notepad++ is even installed, then you also have enough control register it as the default program for the files types your using, meaning you can skip selecting a program name at all:

Process.Start(briefingFile);
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • As i stated in teh initial post, I also tried with the full path to notepad++: "c:\\program file (x86)\\notepad++\\notepad++.exe". I noticed that actually notepad does open but it tried to create a new file adn as I wrote, this file that i try to read does exist since i can open it with notepad. – dandan21 Feb 23 '22 at 19:22
  • Sorry, the post was incomplete (I accidently submitted the form early, before I'd finished adding that part). Must've bumped shift+enter or something. – Joel Coehoorn Feb 23 '22 at 19:23
  • I don't understand what you suggest I do with the space in program files? why is this relevant. Just for trying I tried to specify notepad (link) full path which is under "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories" note the space in "Start Menu" and it was working well. – dandan21 Feb 23 '22 at 19:30
  • 2
    `C:\Program` is the first part of `C:\Program Files\...`, and you see that error because **something** failed to account for the in that folder name. – Joel Coehoorn Feb 23 '22 at 19:31
  • perhaps i need to specify the input file for notepad differently? my input file in "program files" as well – dandan21 Feb 23 '22 at 19:32
  • The missing files error is because the computer is looking in your current working directly, which is the location of your original executable file as determined by the Visual Studio compiler. You can change the current working directory in code, or be more precise in specifying your path for those files. – Joel Coehoorn Feb 23 '22 at 19:33
  • @JoelCoehoorn Then how come it does not fail when I specify "notepad.exe" instead of notepad++.exe ? – dandan21 Feb 23 '22 at 19:33
  • I am using full path to the file I try to open. However, as I wrote, it opens fine with notepad which means teh path is legit and the file CAN be found – dandan21 Feb 23 '22 at 19:35
  • Throwing an exception might trigger logging etc and lead to much more IO than a simple `File.Exists()` tho – Charles Feb 23 '22 at 20:17
  • I just tested this, 100 calls to `File.Exists` took 1.52ms. Throwing and catching 100 Exceptions took 3500 ms. Would have surprised me if suddenly throwing exceptions would be cheap. – Charles Feb 23 '22 at 21:33
1

The solution is to use double quotes in the text file, not in the application. In my case: change:

Process.Start(editor, briefingFile);

to

Process.Start(editor, $@"""{briefingFile}""");

The "editor" is a full path to the editor.

dandan21
  • 37
  • 1
  • 7
0

There is nothing wrong with your code, although, as Joel stated, there are drawbacks to using File.Exists(). You should only need to make sure that the Notepad++ folder is in your User/System Environment PATH variables. I added the path for my Notepad++ folder on this PC, which is just C:\Program Files\Notepad++\, and ran the same code and it opens the file in Notepad++ just fine.

I ran the below code in an empty .NET 3.1 forms project and it can execute just fine. Do you still get your error in a new project?

        OpenFileDialog fileDialog= new OpenFileDialog();
        fileDialog.Filter = "All Files (*.*)|*.*";
        fileDialog.FilterIndex = 1;
        string editor = "";
        if (fialDialog.ShowDialog() == DialogResult.OK)
        {
            editor  = fileDialog.FileName;
        }

        //Added the escaped quotes to the front & back of this in case the path contains spaces.
        var filePath = @"""C:\SOMELOCALFILEPATH\test.txt""";
        if (File.Exists(filePath))
        {
            Process.Start(editor, filePath);
        }

I set a break point and my editor is in this format when Process.Start executes: "C:\\Program Files\\Notepad++\\notepad++.exe"

  • Thanks. However, I still want to do know to specify a full path to a text editor to open the file with. In my case I let the user choose his preferred app to open the text file and store this variable. I was thinking I could specify the full path and the app would open. Does this mean I also have to add the selected app the the PATH variable? I'd rather not change the user PATH. – dandan21 Feb 24 '22 at 06:18
  • Further I think the problem is not finding notepad++. I noticed that notepad++ DOES open but instead of opening the text file, it tries to create a new file. Yes the text file exits since it opened file in notepad... – dandan21 Feb 24 '22 at 06:39
  • That makes sense. Can you share the parts of the code where you assign those strings to `editor` & `briefingFile`? I thought I saw somewhere that you needed to not reference the path directly, but a direct reference works for me, so I am guessing that there is something wrong with how you are assigning those variables? – JediOfTheShire Feb 24 '22 at 14:31
  • editor is derived from a browse button... 'var searchForFile = new OpenFileDialog(); searchForFile.Title = "Select default text editor"; var result = searchForFile.ShowDialog(); ... selectedFile = searchForFile.FileName;' file name is of the sort @"path\path\filename" Still, I think the problem is in teh way the app, in this case notepadd++ is built and not my code. After all notepad has not issues with spaces in the file name. "Program files..." is just file. However notepad++ does not – dandan21 Feb 24 '22 at 16:50
  • Can you start a brand new project and drop that code I updated my answer with into a button and see if it gives you an error? If that causes a bug on your machine then I will be at a loss. – JediOfTheShire Feb 24 '22 at 17:15
  • Your code is working with the change that the double quote should be on the textfile and is not required on the editor. I think since the editor interpretation is done by the .NET code while the texfile is done by the editors code. perhaps notepad++, since it is also a unix app, required quotes in the input file if it has spaces in it – dandan21 Feb 24 '22 at 18:56
  • did you try with spaces in the file name? – dandan21 Feb 25 '22 at 08:31
  • Yep, I made the change in the answer. Looks like you need the quotes in the file path if there are spaces in it. Not so for the .exe though. This works for me: `Process.Start(@"C:\Program Files\Notepad++\notepad++.exe", @"""C:\SOMELOCALFILEPATH\test test.txt""");` – JediOfTheShire Feb 26 '22 at 15:54
0

I ran into this issue and had also exactly same two errors when I tried to open a .txt file which was installed under C:\Program Files (x86)\MyProgram\SecondFolder\MyTxt.txt

The problem was when I copied the file path from windows explorer and pasted it into Visual Studio it deleted the blank space between Files and (x86).

copy the file path

File path pasted into Visual Studio

happens when pasted directly into VS to edit it later. Can be avoided if you paste the path into quotation marks

You have to check if the file path is correct. Otherwise it won't work.

Andi Iton
  • 3
  • 2
  • So you made a typo... typos are basic errors and thus considered too general for stack overflow. Of course you should check what you type – JHBonarius Aug 07 '22 at 10:43
  • No I didn't make a typo...I copied the file path directly from win explorer but when you paste it into visual studio it deletes (for whatever reason) this one blank space between Files and (x86). – Andi Iton Aug 07 '22 at 10:48
  • Aha, ok, that might indeed be a good issue. Let me check (although I cannot for the next 8 hours. So later) – JHBonarius Aug 07 '22 at 10:56
  • I played around and tested it a little bit. This happens when you directly paste the file path into Visual Studio because of "Program Files". The editor formats it automatically and it can be easily overseen. It can be avoided if you paste it into already existing quotation marks in Visual Studio. – Andi Iton Aug 07 '22 at 11:39
  • Ah yes, from that perspective this is a basic typo question. If you just copy paste text in a code context instead of a string context, visual studio will automatically (if enabled, which is the default) format it as code. This is a user error. – JHBonarius Aug 07 '22 at 15:08