Tuesday, May 30, 2017

Process.Start() exits right away on Windows 7

Leave a Comment

Process.Start("d:/test.txt"); //simple .txt file works perfectly fine on Windows 8 onward but on Windows 7 (x64) it starts the process and immediately closes it.

I've already tried the following:

  1. Calling through ProcessStartInfo and setting CreateNoWindow=true, UseShellExecute=true and Verb="runas" (though not sure why I had to set this one).

  2. Tried attaching Exit event and it confirms that the process does start but it exits right away and I don't even see the Notepad window open for a blink of a second.

Edit: I've tried it with an image files and few other extensions and they open just perfect. Something wrong with just the .txt files (and/or probably other formats).

4 Answers

Answers 1

You're saying your code is working fine in other OS and other file formats even in Win 7.

Let's try following checks to verify if things are correct

  1. Verify if notepad.exe is in path Start -> Run -> notepad.exe should launch Notepad
  2. Double click on a .txt file and see if it automatically opens in Notepad
  3. Verify if Process.Start("notepad.exe") starts an instance of Notepad
  4. var process = Process.Start(file used in step 2); and verify the returned process info in the debug mode and see if says the newly created process is still running or not.

Answers 2

I've had this happen on Windows 7 before. It's likely that your Path environment variable has become corrupted. The maximum number of characters that can be used in the Path variable is 2047. Installing many executables on your machine can overflow the Path variable. Here is a SO discussion that shows some ideas to get around it:

How do you avoid over-populating the PATH Environment Variable in Windows?

If you just need to get notepad running again quickly, you can modify the Path environment variable and just put the system location to Notepad at the beginning of the variable. (ex. "c:\windows\system32\notepad.exe").

And if you're not sure how to modify your Path variable, here is a good how-to: http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx

Answers 3

Its definitely an issue with file association. I have tried it windows 7 and it works fine. Try double clicking on the file and check if it opens in notepad, if it doesn't then configure it to open via notepad.Also you should check the exception that it throws, If File association is missing then it will launch the Openwith dialog.

If it is associated with wrong program then you can change it manually.

If you want to find association type pragmatically then, I would suggest looking at this answer.

How to I get file type information....

Answers 4

What about just using

Process.start("start", "d:\\test.txt") 

or

Process.start("explorer", "d:\\test.txt") 

or

Process.start("cmd", "/c notepad.exe d:\\test.txt") 

If it still doesn't work, try using the straight shellexecute, as described here Executing another program from C#, do I need to parse the "command line" from registry myself?

https://www.gamedev.net/topic/310631-shellexecuteex-api-call-in-c/

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment