Process.Start() exits right away on Windows 7
Asked Answered
F

5

9

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).

Fascinating answered 21/5, 2017 at 20:17 Comment(13)
try Process.Start("notepad.exe", "d:/test.txt");Hayton
This also doesn't work. And I want to keep it generic i.e. any file extension should be opened.Fascinating
Have you tried opening the text file through Windows Explorer? Perhaps your file associations have been removed for the .txt extension.Han
Process.Starts(document) starts whatever is associated with the document so it's "generic" if you will, but may vary considerably per user/machine. Shell execute has a very complex algorithm that's defined by the user. Nothing garantees it will run notepad for example. Plus it also has security implication. If you run as admin or not may also change the game.Bayles
@SimonMourier I'm only having the issue with .txt files when Process.Starts(document) is called and that too on Windows 7. I've tested on at least three Win7 x64 systems. Text files (and of course other common extensions tested) open fine on Win 8 and onward.Fascinating
That code works fine on my Win7 box as it's supposed to. It may also be because your program is compiled as x86 and the OS (so as the shell, or what it points to) is x64.Bayles
This is not due to the OS... Do you get any exceptions? Are you sure the current user has access to the file? Does it work if you run elevated? Does Process.Start("notepad"); work? Can you run Notepad on that machine? I suggest you try creating a new user and try the same code from the new account.Millardmillboard
Is "d:" the system drive with windows installed or a second drive? Is it a real HDD or a mapped network drive? What program is associated to txt on the machines you used? notepad.exe or a substitute?Abscise
What happens if you place the txt-file in a subdirectory like d:/temp/test.txt. What I am thinking about is it could be a UAC problem as Win 7 introduced that you are not allowed to edit files in windows installation drive root directory. You can get around that by creating a subdirectory.Abscise
@SimonMourier Currently I'm compiling it for "All CPU". I'll try compiling it as x64 on x64 machineFascinating
Is your project a windows service or a forms project?Nival
This sounds like a UAC issue; try moving 'test.txt' to %PUBLIC% or %APPDATA%. Or try running your program as administrator.Confectioner
Just in case because I noticed. You are using a "/" as separator for directories. Try using a "\" and don't forget to escape it (so essentially use "\\" like Process.Start("d:\\test.txt"); (don't use \t - t with single backslash as this is escape for a tab sign). I know the slash "should" work but I remember problems on "earlier" windows .Abscise
F
1

I was able to solve this bug just by changing build platform from AnyCPU to specifically x64 (my target machine is x64). This is strange but it solved the problem! Thanks Simon Mourier for this tip.

Fascinating answered 11/6, 2017 at 10:55 Comment(0)
L
0

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....

Libbi answered 29/5, 2017 at 11:49 Comment(2)
It's not an association issue.Fascinating
In that case, can post the exception that you are getting.Libbi
M
0

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.
Marinemarinelli answered 29/5, 2017 at 15:3 Comment(0)
E
0

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

Eatables answered 30/5, 2017 at 13:21 Comment(2)
It's a fresh installation of Win7Fascinating
Ok, but this problem could still be related to the Path variable. Can you check your Path variable for notepad.exe or copy/paste the contents of your Path variable here? What happens if you open a command line and just type "notepad"? Does notepad open?Eatables
E
0

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/

Expeditious answered 30/5, 2017 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.