none
Opening a file WITH your VB application RRS feed

  • Question

  • Hi everyone,

    Does anybody know how I would go about opening a file with my VB.NET application?

    My application uses data it reads from text files. On the first form there is a button to bring up an open file dialog which pulls a text file in. But how would I get it to automatically do that when I "Open With" my application.

    I know I can tell Windows to open a .txt file using my VB application's .exe, But once it does, my application doesn't know what to do with it.

    Essentially, how do I tell my VB application what to do when a Windows opens a file directly with my application. Does anyone know if it's possible / how complex it is?

    Thanks,

    A

    Wednesday, September 6, 2017 11:04 AM

Answers

  • When your app launches after clicking "Open With", or if the file extension is associated with your app, Environment.GetCommanLineArgs will provide you with the name of the file. From there you will have to write the code in order to open the file and process it.

    There many different ways to open the file depending on its structure and what you need to do with it. You will have to be more specific if you want help with that.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Frank L. Smith Wednesday, September 6, 2017 11:50 AM
    • Marked as answer by Aronics Wednesday, September 6, 2017 2:38 PM
    Wednesday, September 6, 2017 11:42 AM

All replies

  • When your app launches after clicking "Open With", or if the file extension is associated with your app, Environment.GetCommanLineArgs will provide you with the name of the file. From there you will have to write the code in order to open the file and process it.

    There many different ways to open the file depending on its structure and what you need to do with it. You will have to be more specific if you want help with that.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Frank L. Smith Wednesday, September 6, 2017 11:50 AM
    • Marked as answer by Aronics Wednesday, September 6, 2017 2:38 PM
    Wednesday, September 6, 2017 11:42 AM
  • Open a file for what?

    If you don't know what to do with it, there is no need to open. 

    Your program does not know it for sure, it is just a set of instructions from a programmer.


    Success
    Cor

    Wednesday, September 6, 2017 12:13 PM
  • Thank you Paul! That was very helpful. 
    Wednesday, September 6, 2017 2:38 PM
  • if you want to ope a text file 

    imports system.io

    imports system.io.file

    dim filepath as string="C:\myfile.txt"

    dim infile as new streamreader

    while n

    infile=opentext(filepath)

    imports system.io
    
    imports system.io.file
    
    dim filepath as string="C:\myfile.txt"
    
    dim infile as new streamreader
    
    while not infile.endofstream
    
    infile=opentext(filepath)
    dim entireline as infile.readline.Split(","c).ToString
    textbox.text=entireline
    end while

    Wednesday, September 6, 2017 6:46 PM
  • if you want to ope a text file 

    imports system.io

    imports system.io.file

    dim filepath as string="C:\myfile.txt"

    dim infile as new streamreader

    while n

    infile=opentext(filepath)

    imports system.io
    
    imports system.io.file
    
    dim filepath as string="C:\myfile.txt"
    
    dim infile as new streamreader
    
    while not infile.endofstream
    
    infile=opentext(filepath)
    dim entireline as infile.readline.Split(","c).ToString
    textbox.text=entireline
    end while

    The text file read will be locked because you didn't dispose the reader.

    You can use a Using block to do it for you (even if an exception is thrown):

    Using rdr As New System.IO.StreamReader(lastDownloadsFile)
        Do While rdr.Peek() >= 0
            Dim itm As String = rdr.ReadLine
                'Now process the line “itm”
        Loop
    End Using


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Wednesday, September 6, 2017 6:56 PM ...formatting
    Wednesday, September 6, 2017 6:55 PM