File associations & File Types Editor
-
I am finishing myfirst application and have assigned a new file extension. I want the allow the user to double click on the file to open the application and then the file that was selected. I have set up the Fiile Types Editor in the setup project, but am unsure how to add code to my application to actually open the file. Can anyone help me? Thanks!
-
I am finishing myfirst application and have assigned a new file extension. I want the allow the user to double click on the file to open the application and then the file that was selected. I have set up the Fiile Types Editor in the setup project, but am unsure how to add code to my application to actually open the file. Can anyone help me? Thanks!
ok so you have associated your file type with your application in folder options > file types? that is step 1.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim fileName As String = System.Environment.CommandLine.Substring( _ System.Environment.CommandLine.IndexOf(Chr(34) & " " & Chr(34)) + 3) 'this is now a string containing the exact path & filename which was opened with the program. fileName = fileName.Substring(0, fileName.Length - 1) 'then in this sub or another, do something with the file: '(in this case, open the file, and copy the text in it into a textbox) Dim inFile As IO.StreamReader = IO.File.OpenText(fileName) textBox1.text = inFile.ReadToEnd inFile.Close() End Sub
all you need here really is the 'fileName' string which i setup for you, and you can do what you want with it, or you can use the exact example above and test it out. let me know if this helps. [many thanks to the Rage man himself.] ------------------------ Jordan. III -
I am finishing myfirst application and have assigned a new file extension. I want the allow the user to double click on the file to open the application and then the file that was selected. I have set up the Fiile Types Editor in the setup project, but am unsure how to add code to my application to actually open the file. Can anyone help me? Thanks!
See Creating a File Association[^] in the Platform SDK on MSDN. Decent installers (like Windows Installer) can do this easily during installation of your application as well (in the case of Windows Installer, see the Extension, ProgId, and Verb tables). In you use a Windows Installer project within your solution for compiled projects, you can also click on the File Types Editor above the Solution Explorer when you select your installer project file.
Microsoft MVP, Visual C# My Articles
-
ok so you have associated your file type with your application in folder options > file types? that is step 1.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim fileName As String = System.Environment.CommandLine.Substring( _ System.Environment.CommandLine.IndexOf(Chr(34) & " " & Chr(34)) + 3) 'this is now a string containing the exact path & filename which was opened with the program. fileName = fileName.Substring(0, fileName.Length - 1) 'then in this sub or another, do something with the file: '(in this case, open the file, and copy the text in it into a textbox) Dim inFile As IO.StreamReader = IO.File.OpenText(fileName) textBox1.text = inFile.ReadToEnd inFile.Close() End Sub
all you need here really is the 'fileName' string which i setup for you, and you can do what you want with it, or you can use the exact example above and test it out. let me know if this helps. [many thanks to the Rage man himself.] ------------------------ Jordan. III -
Thanks! I tried this and got a path without the drive letter. What am I doing wrong? Thanks!
i tried this exact example when i made it for you, on my computer and it worked properly. if for whatever reason it isnt working properly, take the line where the string delcaration for fileName is. edit that line to something like
Dim fileName As String = System.Environment.CommandLine
instead. then test this string (with, ex, a msgbox) to see what it is actually returning when u try and open a file with the program. keep motifying it (like how i did, taking a substring from it) untill it works. i just built a new computer last nite and have yet to reinstall VS.net so i cant help with examples for now. ------------------------ Jordan. III -
i tried this exact example when i made it for you, on my computer and it worked properly. if for whatever reason it isnt working properly, take the line where the string delcaration for fileName is. edit that line to something like
Dim fileName As String = System.Environment.CommandLine
instead. then test this string (with, ex, a msgbox) to see what it is actually returning when u try and open a file with the program. keep motifying it (like how i did, taking a substring from it) untill it works. i just built a new computer last nite and have yet to reinstall VS.net so i cant help with examples for now. ------------------------ Jordan. IIII got the path string right using your suggestion (by building the application from within Studio.NET and looking at the path for the .exe file). However when I tried opening the installed application by double clicking a file, I got an " illegal character in path" error. How can I debug this? Is there a way to set a breakpoint in the installed executable? Thanks!
-
I got the path string right using your suggestion (by building the application from within Studio.NET and looking at the path for the .exe file). However when I tried opening the installed application by double clicking a file, I got an " illegal character in path" error. How can I debug this? Is there a way to set a breakpoint in the installed executable? Thanks!
-
Thanks! I tried this and got a path without the drive letter. What am I doing wrong? Thanks!
Did you get something like this:
Settings\userid\Desktop\filename.txt
If so, go back to your file association and put quotation marks around the %1 marker.
myapp.exe "%1"
RageInTheMachine9532
-
Did you get something like this:
Settings\userid\Desktop\filename.txt
If so, go back to your file association and put quotation marks around the %1 marker.
myapp.exe "%1"
RageInTheMachine9532
I've got the quotes around the "%1". I can't figure our how to break the execution of the code on the installed program, when I double click on the file, so that I can see what the path string actually is. I therefore don't know the path string that is being generated. The files are in a folder on my desktop, so your path is conceptually correct with the addition of the folder name before the filename. I did find a stupid mistake and am working to fix it. I'll post the relevant code as soon as I'm not (too) ashamed of it. Can you tell me how to debug an installed program? Thanks!
-
I've got the quotes around the "%1". I can't figure our how to break the execution of the code on the installed program, when I double click on the file, so that I can see what the path string actually is. I therefore don't know the path string that is being generated. The files are in a folder on my desktop, so your path is conceptually correct with the addition of the folder name before the filename. I did find a stupid mistake and am working to fix it. I'll post the relevant code as soon as I'm not (too) ashamed of it. Can you tell me how to debug an installed program? Thanks!
All you have to do to test your app with command line arguments is go to the Project menu, select Properties, click on the Configuration Properties folder on the left, then select the Debugging item. On the right, you'll find a blank for command line parameters. Just fill it in with the full path of the file your trying to use (without the quotes around it.) RageInTheMachine9532
-
All you have to do to test your app with command line arguments is go to the Project menu, select Properties, click on the Configuration Properties folder on the left, then select the Debugging item. On the right, you'll find a blank for command line parameters. Just fill it in with the full path of the file your trying to use (without the quotes around it.) RageInTheMachine9532
OK, now we are getting somewhere! I can see the path string, but it includes BOTH the path to the application executable and the file whose path I put into the command line as you described. The error is probably the result of 2 drive letters and 2 colons. What am I doing wrong? Thanks again!
-
OK, now we are getting somewhere! I can see the path string, but it includes BOTH the path to the application executable and the file whose path I put into the command line as you described. The error is probably the result of 2 drive letters and 2 colons. What am I doing wrong? Thanks again!
nvmoss wrote: The error is probably the result of 2 drive letters and 2 colons. Nope...The problem is that your using Environment.CommandLine. It will return the ENTIRE command line, including the command that started your app. The solution is use Environment.GetCommandLineArgs(). This will return an array of Strings, the first of which, index 0, will be the command that started the app. Index 1 will be the first argument:
|----------- Index 0 --------------| |------------------- Index 1 ------------------------|
C:\Program Files\myCompany\myApp.exe C:\Documents and Settings\userID\Desktop\fileToUse.txtDim cmdArgs As String()
cmdArgs = Environment.GetCommandLineArgs()cmdArgs(0) will be "C:\Program Files\myCompany\myApp.exe" and cmdArgs(1) will be "C:\Documents and Settings\userID\Desktop\fileToUse.txt", of course, without the quotes. RageInTheMachine9532
-
nvmoss wrote: The error is probably the result of 2 drive letters and 2 colons. Nope...The problem is that your using Environment.CommandLine. It will return the ENTIRE command line, including the command that started your app. The solution is use Environment.GetCommandLineArgs(). This will return an array of Strings, the first of which, index 0, will be the command that started the app. Index 1 will be the first argument:
|----------- Index 0 --------------| |------------------- Index 1 ------------------------|
C:\Program Files\myCompany\myApp.exe C:\Documents and Settings\userID\Desktop\fileToUse.txtDim cmdArgs As String()
cmdArgs = Environment.GetCommandLineArgs()cmdArgs(0) will be "C:\Program Files\myCompany\myApp.exe" and cmdArgs(1) will be "C:\Documents and Settings\userID\Desktop\fileToUse.txt", of course, without the quotes. RageInTheMachine9532
Thanks! I tried the GetCommandArgs(), but arg 1 is only "C:\Documents" I tried a few more args (2,3, etc) and found that everytime there is a space in the path, a new argumant is created. I checked and the %1 in the File Type Arguments Prperty is in double quotes. Am I doing something wrong?
-
Thanks! I tried the GetCommandArgs(), but arg 1 is only "C:\Documents" I tried a few more args (2,3, etc) and found that everytime there is a space in the path, a new argumant is created. I checked and the %1 in the File Type Arguments Prperty is in double quotes. Am I doing something wrong?
Check the File Type association in Explorer first. I'll bet that the quotes are missing. The symptoms you are running into (spaces dictate new argument) say that this is exactly what the problem is. RageInTheMachine9532
-
Check the File Type association in Explorer first. I'll bet that the quotes are missing. The symptoms you are running into (spaces dictate new argument) say that this is exactly what the problem is. RageInTheMachine9532
Did you mean File Types in Explorer, or in the Argument Property of File Types in the Setup project of my application? I checked both. In Explorer, the "Open With" application is my application, and the Argument Property in the Setup Project is "%1". Is there another explaination? Thanks!
-
Did you mean File Types in Explorer, or in the Argument Property of File Types in the Setup project of my application? I checked both. In Explorer, the "Open With" application is my application, and the Argument Property in the Setup Project is "%1". Is there another explaination? Thanks!
In Explorer... Open an Explorer window, on say C:, and go to the Tools menu, pick Folder Options, and click on the File Types tab. Then scroll down to your file extension and click on it. Click on the Advanced button, then click on the Open action and the Edit button. In the "Application used to perform action:" box you should see something like this, but for your app:
C:\WINDOWS\system32\NOTEPAD.EXE "%1"
If the quotes are not around the %1, you'll run into the problem that your having. RageInTheMachine9532
-
In Explorer... Open an Explorer window, on say C:, and go to the Tools menu, pick Folder Options, and click on the File Types tab. Then scroll down to your file extension and click on it. Click on the Advanced button, then click on the Open action and the Edit button. In the "Application used to perform action:" box you should see something like this, but for your app:
C:\WINDOWS\system32\NOTEPAD.EXE "%1"
If the quotes are not around the %1, you'll run into the problem that your having. RageInTheMachine9532
Thanks! I looked as you suggested and the quotes are there. However, there are also quotes around the path to the executable that opens the application. I notice that there are no quotes around this path in your example. Should I remove these quotes in Explorer?
-
Thanks! I looked as you suggested and the quotes are there. However, there are also quotes around the path to the executable that opens the application. I notice that there are no quotes around this path in your example. Should I remove these quotes in Explorer?
Nope. They stay there. When you test your app, put the quotes around the complete path in the Command line args under Project/Properties/Debugging. Opps! My bad. RageInTheMachine9532
-
Nope. They stay there. When you test your app, put the quotes around the complete path in the Command line args under Project/Properties/Debugging. Opps! My bad. RageInTheMachine9532
Thanks! It works now. However, if you open the ap by double clicking on a file, then double click on another file, a second instance of th app opens. is there a way to just add a second MDI Child to the first app instead, or is this the expected behavior? Thanks again, everyone has been very persistent and very helpful!
-
Thanks! It works now. However, if you open the ap by double clicking on a file, then double click on another file, a second instance of th app opens. is there a way to just add a second MDI Child to the first app instead, or is this the expected behavior? Thanks again, everyone has been very persistent and very helpful!
That's expected unless you code it otherwise. Your app is going to have to check for a previous instance that is already running. THis code cample is right out of the VB.NET docs:
' Visual Basic .NET
Function PrevInstance() As Boolean
If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End FunctionYou'll also have to write a mechanism so the app can communicate with another instance of itself. Look into Call Context or Asynchronous Remoting for some examples. They will look kind of wierd and you'll be wondering why your using http to send this stuff back and forth, but it's right. You'll be sending the full filename from the new instance to the old one. RageInTheMachine9532