none
Open a file using default application

    Question

  • Hello,

    Back in VB6, I used the ShellExecute api to open a file using it's default application.  In other words, .XLS files would open in Excel, .DOC files would open in Word, etc.  The call looked like this:

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
     (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _  ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    I realize this may be a stupid question, but is there a better/easier way to do this in .NET?  I find it hard to believe that the old method is still the preferred way to do this.

    Additionally, is there a way to open a file as "Read only"?  Hopefully this would be an option in a newer method.

    I appologize in advance if this silly question has been asked multiple times - I could not find any other method than using ShellExecute.

    Thanks!

     

    Thursday, April 20, 2006 1:26 PM

Answers

  • Success!

    I found that this opened the file in Read-Only mode:

    Dim startInfo As New ProcessStartInfo("Excel.exe")

    'The "/r" is to open the file read only and the

    ' chr(34) encloses the filename in double-quotes in case the path includes spaces

    startInfo.Arguments = "/r " & Chr(34) & sFullFilePath & Chr(34)

    Process.Start(startInfo)

    Just wanted to post this answer in case someone ran into the same thing at some point.

    Friday, April 21, 2006 2:04 PM

All replies

  • After some more research, I think I found a possible route.

    Using this:

    System.Diagnostics.Process.Start(sFullFilePath)

    I can open the file with it's default application.  My only problem now is how to open the file as "Read only".  I have tried to understand processes more, but I am still stumped.

    I tried this:

    Dim myProcess As New Process()

    myProcess.StartInfo.FileName = sFullFilePath

    'myProcess.StartInfo.Verb = "Readonly" - this does not work.  Just reference

    myProcess.Start()

    ..but I can't find the Process parameter or setting that will open the file as read-only. 

    Again, I would greatly appreciate any suggestions.  Thank you.

    Thursday, April 20, 2006 8:55 PM
  • Loki,

      I believe ReadOnly is an aspect of the process you wish to start, and not an aspect of processes per se.  For example, you can open a Word file read only, but a Text file is open by default in Notepad, where no such setting exists.  You can also set a file to be read only, in which case opening with the default application would automatically open in a read only state.

      I'm not exactly sure how to make this work, but I suspect you will need one of three approaches:
    (a) set an application-specific argument in the ProcessInfo.Arguments property.
    (b) set the file to have the read-only flag set.
    (c) set the security permissions for the process to not allow file modifications, effectively starting it in a read-only state.

    Hope that helps,

    Erik

    Friday, April 21, 2006 3:02 AM
  • Erik,

    Thanks for replying.  Unfortunately, the file settings (read-only flag & permissions) need to stay the way they currently are for other users to work with the files properly.  Therefore, the only option that might work for me would be "a".

    I have been experimenting with using a command line argument and this works (when executed from Start, Run this opens as Read-Only):

    "c:\Program Files\Microsoft Office\OFFICE11\excel.exe" /r "C:\Temp\06-04-06.csv"

    However I can't seem to get the "/r" argument to work in code.  I added this line:

    myProcess.StartInfo.Arguments = "/r"

    but it still opens the file normally.  I changed it to just "r" and got the same results.  Am I using this improperly?

    I would appreciate any more suggestions.  Thanx!

    Friday, April 21, 2006 1:09 PM
  • Success!

    I found that this opened the file in Read-Only mode:

    Dim startInfo As New ProcessStartInfo("Excel.exe")

    'The "/r" is to open the file read only and the

    ' chr(34) encloses the filename in double-quotes in case the path includes spaces

    startInfo.Arguments = "/r " & Chr(34) & sFullFilePath & Chr(34)

    Process.Start(startInfo)

    Just wanted to post this answer in case someone ran into the same thing at some point.

    Friday, April 21, 2006 2:04 PM
  • Loki,

      Glad this worked out, and thanks for posting the answer.

    Erik
    Friday, April 21, 2006 2:08 PM
  • Loki70 no offense buddy but I don't think you answered your question.

    Your question was how to open a file using it's file type associated program.

    you are just setting it to start excel then giving it the arguments, if you did the same for a PDF it would not work as per se.

    I unfortunately don't have an answer for you only a "note" to other users that your code is indeed just opening excel and is not checking file associations.

    Cheers,

    Michael Proctor

    Friday, April 21, 2006 5:42 PM
  • Valid point, I suppose. 

    Ideally, I would have preferred a method that handled all file types, but until someone comes up with a way to do this AND open the file as Read-Only, this gets the results I want.

    Thanx

    Saturday, April 22, 2006 1:30 AM
  • It appears that you can specify any file that has an associated application using this form for the method:

    ProcessStartInfo(String, String): Initializes a new instance of the ProcessStartInfo class, specifies an application file name with which to start the process, and specifies a set of command-line arguments to pass to the application.

    Tuesday, May 10, 2011 9:12 PM