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.
Related
I have an IE plugin which adds buttons in a page where there are pdf links and opens them in a specific application when clicked.
Lets say I need to open a xyz.pdf file in abc.exe application. abc is not the default application for file type .pdf.
In one machine the below works
Process p = Process.Start("pathtoabc.exe", "pathtoxyz.pdf");
In another machine it only works if I make abc.exe as the default app and then use the below
Process p = Process.Start("pathtoxyz.pdf");
Can you give me any pointers please? I also tried using ProcessStartInfo with no change
Updates:
I tried using the default Acrobat reader with an argument
Argument for processstartinfo looks like this "C:\PDF Files\Professional-Letters-Guide.pdf"
FileName = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe"
Result - Acrobat reader opens but with an error message "File not found". The is available in the path though.
Solved:
It was a space in the Foldername.. solved it by enclosing the filename with quotes "\""
Thank you all for the suggestions.. they helped me think it through.
Have you tried qualifying the Process.StartInfo.Arguments value with quotes and the full path to the file? What about the WorkingDirectory property? Also, the previous assertion regarding confirmation of the application being called supporting command line parameters is absolutely valid. You can be fooled into thinking that it does due to operating system file extension associations specific to a machine.
The second parameter of the Process.Start is passed to the application you are trying to start and it wont open the file using this application "pathtoabc.exe" unless the application "pathtoabc.exe" accepts the file name as a startup argument.
So you need to check if the application you are trying to use supports this kind of argument.
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 am working on converting word docs and excel to pdf and automate the whole process. We have a server application that prints all the .doc and .xls files using COM interop and we want to have those files converted to PDF instead of printing them. I referred to How do I convert Word files to PDF programmatically? and also http://www.microsoft.com/en-us/download/details.aspx?id=7 to convert the documents to PDF and i was able to convert them. When i was doing some more research on the topic i found that it is not advisable to call Office Interop from Server Side application (Office documents to PDF), so i thought of using something like a PDF printer driver using which i can configure to bypass the Save As dialog. The i downloaded PDF995 and used the following code to test the output
string printerName = "PDF995";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = (#"C:\...\test.XLS");
startInfo.Arguments = "\"" + printerName + "\"";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
//startInfo.UseShellExecute = true;
startInfo.Verb = "printto";
using (Process process = new Process())
{
process.StartInfo = startInfo;
process.Start();
if (!process.HasExited)
{
process.WaitForExit();
}
}
It works in my box and i need to test it in the server. I just realized while testing it, i am getting exception "No Process associated with this object" at the line (!process.hasexited) only for .xls files but not for .doc and .ppt files. I am wondering why, i tried to look at the Process Monitor (https://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) but could not find it. But it does work for .doc files without throwing the exception. Any idea why?
I would also like to know if there is a better way to go about it? I did take a look at the libraries like Aspose to convert word to pdf but the problem was we may have to purchase separate modules for word and another one for excel and one for slides which would be expensive. So we thought of using a printer driver which would be ideal to handle different types of documents.
Trying to open an image for editing in C#
I can open the file with
System.Diagnostics.Process.Start(fileItem.Path);
This seems to issue the default open command which in my case for jpg files is standard preview, is there any way to use Process to open the file with the associated "Edit" command.
You can use the Verb-Property of the ProcessStartInfoclass
See MSDN
ProcessStartInfo startInfo = new ProcessStartInfo("myfile.jpg");
startInfo.Verb = "edit";
Process.Start(startInfo);
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".