Process.Start opens multiple instances - c#

I have the following code:
Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + "\\Internet Explorer\\iexplore.exe", "http:\\\\someurl.com");
It runs fine, but opens two instances of IE (Needless to say, I'm expecting one). Is there something obvious I'm missing here, or is it a more detailed issue? I'm using Visual C# 2010 Express if that matters, and .NET 4 Client Profile.

Try removing the exe path to IExplore and just let Process start the link, in my experience this should open in a new Tab not window
Example:
Process.start("http:\\\\someurl.com");

Related

Process.Start() exits right away on Windows 7

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:
Calling through ProcessStartInfo and setting CreateNoWindow=true, UseShellExecute=true and Verb="runas" (though not sure why I had to set this one).
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).
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.
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....
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
Verify if notepad.exe is in path
Start -> Run -> notepad.exe should launch Notepad
Double click on a .txt file and see if it automatically opens in Notepad
Verify if Process.Start("notepad.exe") starts an instance of Notepad
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.
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
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/

Opening a URL in windows explorer without COM Object

The problem
I'm trying to open a URL in internet explorer. Regularly I would use the Navigate method of the Internet Explorer com object. However that isn't available in the programming language I am using (MapBasic).
My next method would be to use the Shell function to execute a command in the command line. For example:
explorer "http://yahoo.com"
Will navigate to http://www.yahoo.com. However when it comes to longer links, e.g.
https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8
Command prompt actually navigates to:
https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbn
Which is a significantly shorter URL.
To solve this issue I am looking into using the Win32API, but I can't for the life of me figure out how I could run this code with Win32API... Perhaps using Shell32.dll's ShellExecute? Or would it be better to use Kernel32.dll's CreateProcess?
Whichever is used, it would be really helpful if someone could supply me with some example code of how to open the above link with IE using the Win32 APIs in VB/C#. I can easily port the code to MapBasic from there!
(Alternatively if there is a method to open the link through shell, I would love to know how!)
Thanks
You can just "execute" it using Process.Start which is under System.Diagnostics name space.
As Hans Passant replied:
It is 259 characters long. Magic number. Don't use Explorer.exe, use a
browser like iexplore.exe
This is exactly the solution I needed. Using iexplorer.exe instead:
"C:\Program Files\Internet Explorer\iexplore.exe" "https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8"
Opens the URL fine.
Similarly one can also use the following command:
start iexplore "https://www.google.co.uk/imgres?imgurl=http%3A%2F%2Fclv.h-cdn.co%2Fassets%2F15%2F22%2F768x518%2Fgallery-1432664914-strawberry-facts1.jpg&imgrefurl=http%3A%2F%2Fwww.countryliving.com%2Ffood-drinks%2Fa35552%2Ffacts-about-strawberries%2F&docid=kWdFoElV3zVDpM&tbnid=2gR8XfYaBJZV1M%3A&vet=1&w=768&h=518&hl=en&bih=813&biw=1461&q=strawberries&ved=0ahUKEwiV--LfsPbQAhWaHsAKHUfaC2EQMwg0KAMwAw&iact=mrc&uact=8"
Which should be less system dependant.

Crystal report document load hangs

Whenever the program reaches the below code, the program hangs
protected void InitCrystalReport(String _reportUrl)
{
myReportDocument.Load(_reportUrl);
}
This situation only happens when I put the web on IIS (another server), but it doesn't exist when I run the application on Visual studio (Debug mode). I've also used process monitor to monitor the process to see if the file is access denied.
I've tried below on Web Server but none of them works:
Changing application pool to .NET Classic
Restart the printer spooler
Edit
I restarted the server and everything's OK now
I had this problem when i moved a new report to the test server. My fix was to set a specific printer through File.. Print.. to one i knew was on the server (in this case MS XPS Document Writer). Ensure you've used .dispose etc on the object when you're done.
Although this is old question already answered, I also encountered same problem and after wasting one whole day, I got suggestion from #PhilKemreen answer. Here is how I fixed it in my case:
Open report in Crystal Report Designer and Right Click
Select Design > Page Setup..., Page Setup Dialog box appears.
From the printer drop-down list, select Microsoft XPS Document Writer.
Click OK.
Then I deployed the new report file on server, it works.

Open a folder using Process.Start

I saw the other topic and I'm having another problem. The process is starting (saw at task manager) but the folder is not opening on my screen. What's wrong?
System.Diagnostics.Process.Start("explorer.exe", #"c:\teste");
Have you made sure that the folder "c:\teste" exists? If it doesn't, explorer will open showing some default folder (in my case "C:\Users\[user name]\Documents").
Update
I have tried the following variations:
// opens the folder in explorer
Process.Start(#"c:\temp");
// opens the folder in explorer
Process.Start("explorer.exe", #"c:\temp");
// throws exception
Process.Start(#"c:\does_not_exist");
// opens explorer, showing some other folder)
Process.Start("explorer.exe", #"c:\does_not_exist");
If none of these (well, except the one that throws an exception) work on your computer, I don't think that the problem lies in the code, but in the environment. If that is the case, I would try one (or both) of the following:
Open the Run dialog, enter "explorer.exe" and hit enter
Open a command prompt, type "explorer.exe" and hit enter
Just for completeness, if all you want to do is to open a folder, use this:
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() {
FileName = "C:\\teste\\",
UseShellExecute = true,
Verb = "open"
});
Ensure FileName ends with Path.DirectorySeparatorChar to make it unambiguously point to a folder. (Thanks to #binki.)
This solution won't work for opening a folder and selecting an item, since there doesn't seem a verb for that.
If you want to select the file or folder you can use the following:
Process.Start("explorer.exe", "/select, c:\\teste");
You're using the # symbol, which removes the need for escaping your backslashes.
Remove the # or replace \\ with \
You don't need the double backslash when using unescaped strings:
System.Diagnostics.Process.Start("explorer.exe",#"c:\teste");
You should use one of the System.Diagnostics.Process.Start() overloads. It's quite simple!
If you don't place the filename of the process you want to run (explorer.exe), the system will recognize it as a valid folder path and try to attach it to the already running Explorer process. In this case, if the folder is already open, Explorer will do nothing.
If you place the filename of the process (as you did), the system will try to run a new instance of the process, passing the second string as a parameter. If the string is a valid folder, it is opened on the newly created process, if not, the new process will do nothing.
I don't know how invalid folder paths are treated by the process in any case. Using System.IO.Directory.Exists() should be enough to ensure that.
Use an overloaded version of the method that takes a ProcessStartInfo instance and set the ProcessWindowStyle property to a value that works for you.
You're escaping the backslash when the at sign does that for you.
System.Diagnostics.Process.Start("explorer.exe",#"c:\teste");
System.Diagnostics.Process.Start("explorer.exe",#"c:\teste");
This code works fine from the VS2010 environment and opens the local folder properly, but if you host the same application in IIS and try to open then it will fail for sure.
Ive just had this issue, and i found out why. my reason isnt listed here so anyone else who gets this issue and none of these fix it.
If you run Visual Studio as another user and attempt to use Process.Start it will run in that users context and you will not see it on your screen.
Does it open correctly when you run "explorer.exe c:\teste" from your start menu? How long have you been trying this? I see a similar behavior when my machine has a lot of processes and when I open a new process(sets say IE)..it starts in the task manager but does not show up in the front end. Have you tried a restart?
The following code should open a new explorer instance
class sample{
static void Main()
{
System.Diagnostics.Process.Start("explorer.exe",#"c:\teste");
}
}
Do you have a lot of applications running when you are trying this?
I encounter weird behavior at work sometimes because my system runs out of GDI Handles as I have so many windows open (our apps use alot).
When this happens, windows and context menus no long appear until I close something to free up some GDI handles.
The default limit in XP and Vista is 10000.
It is not uncommon for my DevStudio to have 1500 GDI handles, so if you have a couple of copies of Dev studio open, it can eat them up pretty quickly. You can add a column in TaskManager to see how many handles are being used by each process.
There is a registry tweak you can do to increase the limit.
For more information see http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx
System.Diagnostics.Process.Start("explorer.exe",#"c:\teste");
Just change the path or declare it in a string

Windows 7 taskbar - jumplist, jumplistlink and jumplistitem

I am using the Windows API Code Pack for Microsoft .NET Framework to try out of some of the new UI features of the Win7 taskbar. I am coding in C#.
I have a question regarding jumplists. All of the sample code provided assumes that the entries on the jump list are used to call out to run a particular application or open a document, e.g. a text document in a MRU list or run mspaint.exe.
I would like to implement some items which allow me to set state in my own application (i.e. the app which is interacting with the taskbar). MSN Messenger does this, for example, when you can set your status (Busy, Offline etc.).
Try as I might, I cannot create a JUmpListItem or JumpListLink to behave in this way - it treats them as applications or documents.
Does anyone have any samples of how to create an item which raises an event in the same application that created it? I am sure it is simple but I am being very daft.
Many thanks for your help.
I believe what you'd want to do is to call your application with a special set of flags (i.e. launch the executable with certain arguments). At application start up, you'd check to see what flags are set, then send a message to the main instance of the application and then exit out of the new instance.
Using the TaskBarDemo, to open an item created by your application would have to be referenced, ie if your program created a PDF file you would do this:
jumpList.AddUserTasks(new JumpListLink(Path.Combine(systemFolder, "C:\\Program Files\\Adobe\\Reader 9.0\\Reader\\AcroRD32.exe"), "Open Adobe Reader")
{
IconReference = new IconReference(Path.Combine(systemFolder, "C:\\Program Files\\Adobe\\Reader 9.0\\Reader\\AcroRD32.exe"), 0)
});
Otherwise you would have to ensure that your application registered file associations, for recent or frequent items.
I had a few problems with jumplists with the API Pack, i now use VS 2010 Beta 2 and let shell handle the jumplists.
Hope this is helpfull.
These tasks are some sort of IShellLink. Then, you should call ICustomDestinationList's AddUserTasks. Look up samples in Windows 7 Training Kit.

Categories