I have successfully prompted user to select a file in C# using
the openFileDialog control.
I now have the filename, lets call it foo.docx
I want to open the file with the asssoicated app.
i.e., if it is a docx file, launch with word.
Is there a best way to just pass the filename and it do the launch ?
I used System.Diagnostics.Process.Start(openFileDialog1.FileName.ToString());
TIA.
Ralph
Simply use
Process.Start(filename);
This will open the program in the default program set in Windows.
Also, you can use the same to open a URL in the user's default browser.
Just call Process.Start with the file name - the OS will select the associated application.
Process.Start(#"path to\foo.exe");
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 am trying to open a document with Process.Start in an ASP.NET web application. In this case, I am opening a Word file (.docx), but will be opening any type of file in the future.
My understanding of Process.Start is that the application is determined from the extension of the file path passed as a parameter, and opened.
The code I am using is very simple:
Process.Start("C:\\test.docx");
The file exists, and I am not getting any exceptions when the code is run. However, Word is not opening.
I have monitored my running processes through task manager whilst the code is running, and have noticed that a WINWORD.EXE process is starting with the User Name of the application pool being used (DefaultAppPool) in this case.
Why would the process be starting, but no Word window opening?
Edit: In case there is a better solution, here is my situation:
I am allowing users to upload documents, which are saved in an Oracle database as a BLOB. The user is then able to view their saved documents, and open them. What is the best way to extract a byte array from a BLOB, and open it using the correct application?
I have a windows forms application with a custom file extension set up. I am able to save data to my file, and when I double-click my saved file from Windows, it launches my application.
I have not, however, been able to get the name of the file I clicked on to read in its data. Everything seems to tell me args[0] should be the exe (as I'm seeing), args[1] would be the next parameter (probably what i'm looking for; the file name I clicked on) but args.Length is always just 1, whether I open the exe directly or click on a text file that launches the exe, I never have the file name I clicked on.
Edit (resolved; ish): OK, finally have a more specific issue nailed down. My application was deployed with ClickOnce, and I set up all the file associations through the windows forms application properties. When I right-click and view the properties of my saved custom file, it says "Opens with: ClickOnce Application Deployment Support Library" and not my application name. If I change the default to open with my .exe, magically it has the correct arg values (the exe, followed by the file name I clicked on).
You can't access command line arguments for ClickOnce applications directly. To get to them, I used the following, modified a bit from here:
System.Runtime.Hosting.ActivationArguments args = AppDomain.CurrentDomain.SetupInformation.ActivationArguments;
if (args.ActivationData != null)
{
foreach (string commandLineFile in AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData)
{
MessageBox.Show(string.Format("Command Line File: {0}", commandLineFile));
}
}
This gave me the file name I clicked on. Hoorah.
I'm having a little problem figuring out the best way to open up a file that I have stored in a database. The file is being stored as a byte array in a nvarbinary field in the database. Currently when I want to open up a file I use an ASP.NET webpage that I pass a variable to and write the file stream to the page. This works fine when using the in browser version of the Silverlight application, but when out of browser I cannot invoke a browser window to open because I don't have access to dom.
How can I open the bytearray from Silvelright without invoking a browser window? I'm able to pass the bytearray and file type to the Silverlight app no problem. I just don't know how to display it once I have it there..
Thanks!
If you are targeting windows (with full trust enabled, and not mac), you can do this out-of-browser by first writing the file to disk (either in isolated storage or in My Documents), then using a WScript.Shell COM object to have the OS open the file.
After you have saved the byte stream to a file and have the file location, you can do:
using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
{
shell.Run(fileLocation); //works similar to start -> run -> filename
}
If you want to leverage your existing ASP page, you can pass its URL to shell.Run and the OS will use the user's default browser to open that page.
On a mac, the best you could do is save the file to their user directory, and have them manually navigate there with finder and double-click it.
How can I get the path of a file on my computer or on the local area network.
I would to show a window that allows me to browse the file system when I click a button, and I want to select a file and get the path to the file. How can I do this?
P.S. I'm not looking to upload the file; I just want to get the path.
The web application is running on the server, and you don't have access to the client file system at all. Can you imagine how much of a vulnerability that would be? I know I don't want the sites I visit inspecting my file system...
EDIT: Question is for a WinForms application
For a WinForms application, you can use the OpenFileDialog, and extract the path with something like this:
If you're looking for the file path:
string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt
If you're looking for the directory path:
string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder
In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.
To do this in VB.NET use the FolderBrowserDialog.
Due to security restrictions browsers include for user safety, you can't manipulate the client file system directly. You can only use to let them pick a file, and even then it only sends the filename, not the whole path.
The FileSystem API in HTML5 allows for file manipulation, but only within a sandbox for your site specifically, not browsing across the network or other files on the client system.
Instead, provide your users easy steps on how they should use My Computer (or whatever equivalent on other OS's) to navigate to the file and copy & paste the path into a simple text input box.
Have you taken a look at the Path class from System.IO ?