• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

Open a file via double click in explorer

This is my first question ever:

I made a notepad-like application in VB 2005,  it can edit, save, and open plain text files with a ".jmt" extension.
The problem I have (as others here had) is that I want my app to open the jmt files when double clicked in explorer and I just don't know how to, after associating the file extension in "File Types" I had the program to open, but the file is not loaded. I tried all the solutions I found here, but the code always has some kind of compatibility problem with vb2005. I'll really appreciate the experts' help on this matter, I'm totally new to programming.
Thanks in advance.
0
jorgemgonzalez
Asked:
jorgemgonzalez
  • 5
  • 5
  • 2
1 Solution
 
oleggoldCommented:
The simpliest way is to associate the extensions jmt with Yopur app in explorer shell,just open file with jmt extension and in the windows dialog choose Your app,You may need browsing to its path
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try something like...

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim args() As String = Environment.GetCommandLineArgs

        If args.Length > 1 Then
            ' a filename was passed in
            ' put it into a string
            ' args(0) is always the name of the executable
            args(0) = ""
            Dim fileName As String = String.Join(" ", args).Trim

            Dim fi As New System.IO.FileInfo(fileName)
            If fi.Exists AndAlso fi.Extension.ToUpper = ".JMT" Then
                ' do something with the file
                ' call some routine with fi.FullName
                processFile(fi.FullName)
            End If
        End If
    End Sub

    Private Sub processFile(ByVal fileName As String)
        Debug.Print(fileName)
    End Sub
0
 
jorgemgonzalezAuthor Commented:
Thanks for the fast response!

Well, I had an unhandled exception, this is the message:

************** Exception Text **************
System.NotSupportedException: The given path's format is not supported.
   at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
   at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.FileInfo..ctor(String fileName)
   at JMGNotepad.Form1.Form1_Load(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I tried: args(0) = "JMG Notepad.exe"
then I tried: args(0) = (My.Application.Info.DirectoryPath) & "JMG Notepad.exe" with the same results

Good news: The code was accepted by VB2005 :-) and the app. somehow responded upon file double click. The bad news above.

Should I try "As String" at the end of the args(0) line?

Please help.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You misunderstood my comment...  =)

It should be:

    args(0) = ""

From here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemEnvironmentClassGetCommandLineArgsTopic.asp

    "The first element is the executable file name, and the following zero or more elements contain the remaining command line arguments."

So your executable fullpath name is was already in args(0), and what I was doing was ERASING it from the array.

That way when I used:

    Dim fileName As String = String.Join(" ", args).Trim    

"fileName" would contain only the name of the file that was double-clicked and passed into your application.

Hope that clears it up!
0
 
jorgemgonzalezAuthor Commented:
Sorry about the missunderstanding.

I asume the code should have been pasted "as is", in that case, when I double click on a jmt file the program opens as before, but the file is not loaded into the textbox, nor the file name appears in the form text as when a file is opened through the Open command.

Am I missing something? Please bear with me, like I said before I'm totally new to this endevours =)
0
 
oleggoldCommented:
The explorer shell assosiation can be done through  registry too,You just need to write the key under HKEY_CLASSES_ROOT
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...my code is in no way "integrated" with your existing code.  It just demonstrates how to get the filename passed into the application via command line parameters.

In my example, I passed the fullpath to the file into a sub called processFile():

    processFile(fi.FullName)

You need to take "fi.FullName" and do something with it.

How are you loading the file when you use your "Open" command?...
0
 
jorgemgonzalezAuthor Commented:
Thanks a lot Idle Mind!

I got the message, the problem is that I open files through a function. (below)

Private Function Open() As Boolean

        Dim str As FileStream
        Dim sr As StreamReader

        If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.fileName = Me.OpenFileDialog1.FileName
            str = New FileStream(Me.OpenFileDialog1.FileName, FileMode.Open)
            sr = New StreamReader(str)
            Me.TextBox1.Text = sr.ReadToEnd
            sr.Close()
            str.Close()
            Me.Text = fileName & " " & "-" & " " & "JMG Notepad"

        End If

    End Function

Now, how can I work around the OpenFileDialog so it doesn't show up and load the content of the file into the text box? If I invoke Open() or Call Open() the dialog is going to show up.

The solution to that issue is all I need to complete this application.

I really appreciate your time and patience.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something like...

Imports System.IO

Public Class Form1

    Private fileName As String

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim args() As String = Environment.GetCommandLineArgs

        If args.Length > 1 Then
            args(0) = ""
            Dim fileName As String = String.Join(" ", args).Trim
            Dim fi As New System.IO.FileInfo(fileName)
            If fi.Exists AndAlso fi.Extension.ToUpper = ".JMT" Then
                OpenFile(fi.FullName)
            End If
        End If
    End Sub

    Private Sub Open()
        OpenFileDialog1.Title = "Select a JMG file"
        OpenFileDialog1.Filter = "JMG files (*.jmt)|*.jmt|All files (*.*)|*.*"
        If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            OpenFile(Me.OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub OpenFile(ByVal fileName As String)
        Me.fileName = fileName
        Dim sr As New StreamReader(fileName)
        Me.TextBox1.Text = sr.ReadToEnd
        sr.Close()
        Me.Text = fileName & " - JMG Notepad"
    End Sub

End Class
0
 
jorgemgonzalezAuthor Commented:
I'm sorry, I'm afraid my explanation wasn't really good.

What I want is the file opened in the text editor, like in Notepad when you double click on a .TXT file, but without showing thw Open File Dialog at all! =)

I managed to load the content of the file (the string) on the text box, but not the actual file with the subsequent ability of resaving, (Only save as) far from what I need, using:

.. Then

OpenFile(fi.FullName)

------------------------------------------------------------------------------

Private Sub OpenFile(ByVal FileName As String)

Dim fi As New System.IO.FileInfo(FileName)
        Dim objStream As New FileStream(fi.Fullname, FileMode.Open)
        Dim objReader As New StreamReader(objStream)
        Dim strBuffer As String

        strBuffer = objReader.ReadToEnd

        objReader.Close()
        objStream.Close()

        Me.TextBox1.Text = (strBuffer)

End Sub

Remember, all my code has to start: Dim My.Experience As VeryLimited =)

I need the OpenFile Sub without the dialog. I'm already looking at the "Accept" button.

Cheers
0
 
jorgemgonzalezAuthor Commented:
Sorry, I didn't notice the "Private Sub OpenFile". Done, It works!

Thanks a million!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...glad we got it working for ya...  =)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now