I want to open application directory with button click. i get such error
Does anyone have an idea?
If you set UseShellExecute to true, then you can use Process to open a directory. For example, this will open the C:\ drive. You can specify any path you want.
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = #"C:\";
process.Start();
This is similar to using the Run dialog from the start menu. For instance, even though a Word document is not a program, using Shell Execute will allow you to "Start" a word document by using whatever program is associated with it. Likewise the same with a directory.
Have you tried "explorer.exe {0}" ? Explorer is the process you want, and the argument your intended path.
Try setting the ProcessStartInfo.Verb to "Open".
Related
I'm currently making a program, that essentially needs to open a link in Edge, take a screenshot and then close the browser again.
1st issue:
I can open the browser just fine, but it just opens a new tab instead of a new window, if the browser's already open.
I do not want to interfere with an already existing open Edge browser, that our users may be using, but instead open a completely new instance, take a screenshot and then close it again.
I tried using the following, with no luck - it still just opens a new tab
Process proc = new Process();
proc.StartInfo.FileName = "microsoftedge.exe";
proc.StartInfo.Arguments = "http://172.31.44.1/#/cameras" + " --new-window";
proc.Start();
2nd issue:
When trying to kill the process using proc.Kill() I end up getting a system.invalidoperationexception cannot process request because the process has exited , but the browser's still open
Any help is appreciated!
Thank you in advance
Check this out:
using System.Diagnostics;
Process proc = new Process();
proc.StartInfo.FileName = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
proc.StartInfo.Arguments = " --new-window http://google.com";
proc.Start();
It seems as though Chrome (and therefor also Edge) has changed the behavior of this recently.
It used to be the case that you could use the parameter "--no-service-autorun" to avoid the browser closing the original process, and to avoid you ending up with an invalid (dead) process id.
This doesn't work anymore.
When "Startup boost" is disabled in Edge, the first window does have the correct process id, but anything after that is still invalid.
The only thing that I could find that works, is to use "--user-data-dir" parameter to give each process it's own profile and process.
For example, use "msedge.exe --user-data-dir=C:\test123" (make sure each process has a unique directory).
i'm trying to find the most generic way to understand if a file is being "used" by any application.
this is true also for applications like notepad/onenote/notepad++ (which don't lock the file).
i'm handling the opening of the files through my app (using Process.Start), and i need to delete the file when the user finishes working on it.
what i managed to come up until now is the following:
"MSWord like applications" - which has a lock on the file constantly, thus - i know that it's currently being used - and the delete will fail.
"Notepad/MSPaint like application" - which opens a file in a distinct process, and have a lock on the hosting folder - i can check who's locking the folder, and compare the PID to the PID i received when opening the file
"OneNote" like applications - which is problematic as when i open the file - i get different PID (i guess that the process opens, and then it sends the file to the single main onenote process). BUT - i can then check it by name and not PID (not the best, but ok) - as i found out onenote also has some kind of lock on the directory.
the only issue here is that i'll have to wait until the process is closed, then delete the file.
i'm totally lost about "Notepad++" applications or similar to them.
notepad++ doesn't have any lock on the file or folder, and it uses single process.
i 'm not sure how to handle this scenario, when doing Process.Start i don't even have a name of the process. (i can check the registry maybe to see who's the default app that opens the file)...
so, any other directions?
thanks!
== EDIT ==
well, currently i've decided about the following logic, unless there's a flaw in it:
When opening an appliction (Process.Start) - save the PID and the application path. then wait 1 second (or something else i'll think about) - try to get the process by PID - if it exists, this is indeed the PID i want to wait on. if not, then i'll go by process path/name
check if the file is locked by regular lock (by any application) - if so, then don't delete the file yet.
if the file is not locked, check who's locking the folder - then compare it to the PID i have. if the PID doesn't exist (and it's indeed the PID i want to wait on) - then delete the file. if it exists - don't delete the file.
If when starting the process after 1 second the PID doesn't exist, and no lock on the folder/file - i'll just wait until the process ends
that's the idea i think
I am guessing you are trying to do this,
static void ShowFile(string fileToShow) {
using (Process proc = new Process()) {
ProcessStartInfo psi = new ProcessStartInfo(fileToShow);
psi.UseShellExecute = true;
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
}
}
But, maybe you should do this
static void ShowFile(string fileToShow) {
using (Process proc = new Process()) {
ProcessStartInfo psi = new ProcessStartInfo(#"C:\Program Files (x86)\Notepad++\notepad++.exe", "-multiInst " + fileToShow);
psi.UseShellExecute = false;
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
}
}
And then when your function returns, you can delete your temp file.
I understand that you might not know if the system already has notepad++ installed and its path, etc. In that case you can try to use the path for notepad.exe which comes with windows.
The thing is, there is no perfect solution to what you are looking. To put it in other words, no file viewer/editor is guaranteed to
Use a specific locking mechanism
Keep handle open to the file or directory containing it
Not communicate using DDE, etc
You can find out the default application registered to open your file, but you don't know if it starts as a single instance or a multi-instance app or they communicate using DDE, etc
I was keen on using the Process class [C#] to open "Add Printer" wizard, but I was wondering what is the location of the wizard's exe? And if so, is there a way to open straight into the "Add a network, wireless or Bluetooth printer" section of the wizard?
I would be glad if someone can help.
Thanks
You can do what you want by running a specific entry point in shell32.dll via rundll32.exe. The one you want is AddPrinter. For example;
ProcessStartInfo psi = new ProcessStartInfo {
FileName = "rundll32.exe",
CreateNoWindow = true,
Arguments = "shell32.dll,SHHelpShortcuts_RunDLL AddPrinter",
UseShellExecute = true
};
Process.Start(psi);
From a console run rundll32 printui.dll PrintUIEntry for a help dialog describing available command RunDll command lines.
To launch the Install UI you need to execute rundll32 printui.dll PrintUIEntry /il
(/ip for a Network Printer)
If I have a path of any kind of file (.doc , .pdf , .png ...etc) and I would like to open that file as it is opened via double click (no need to determine the host program). An example of what I mean is: .doc file needs to be opened via MS Word or whatever word processor exists in the machine and it is set as defualt word processor.
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx
Process proc = new Process();
proc.StartInfo.FileName = "file.doc";
proc.StartInfo.UseShellExecute = true;
proc.Start();
Use Process.Start and pass the file name as an argument. This requires that the file extension be associated with the correct program.
In my application, if the user saves a file to a folder they don't have permissions for, File.Copy will fail. An example is saving a document to the C:\ root.
Instead of denying access, I'd like to prompt the user to elevate permissions with a UAC prompt, but only for this save function (not for the entire application). Is there a way to do this?
In short... no.
The entire process needs to be elevated and that elevation needs to happen at startup. However! You can create a separate process to do this work. Make a separate .exe that does only this work and gets everything it needs in the command line parameters. You can add verbs to the process that will cause it to be elevated:
Process p = new Process();
p.StartInfo.FileName = "copy.exe";
p.StartInfo.Arguments = new [] { pathFrom, pathTo };
p.Verb = "runas";
p.Start();
Something like that...