[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

Easy Question: Opening Up File with EXE (filename.exe "file.txt")

Ok, I think this is a easy one.

I have this application that I've written that:

- Prompts me for a file using the OpenFileDialog.
- Opens the file.
- Traverses the file.
- Does it's thing.
- Closes the file and provides a results set within a textbox.

What I want to do is be able to from either a command line or something else, type in the EXE with a trailing file name.  Like "c:\temp\exefile.exe textfile.txt" (the results set will be handled differently in this scenario).  I know how to do that in Winbatch (use the %param% command) but I've never tried that in .NET.

Chris, Baltimore
0
clebo99
Asked:
clebo99
  • 5
  • 2
1 Solution
 
PaulHewsCommented:
For Windows apps, the command line parameters are space separated, except when embedded spaces are contained and then they are delimited with quotation marks.  Eg.

my.exe  "C:\This is a long filename.txt" -thisswitch=on


If your file uses Sub Main as a startup object, then you can use the parameter array of Sub Main, like so:

Public Sub Main(ByVal Args() As String)

The Args array will be an array of space delimited arguments to your application.

If you are using a main form as the startup object, it's a little different.  

For Each arg As String In Environment.GetCommandLineArgs
            Debug.WriteLine(arg)
        Next
You will see that the first argument is actually the EXE path, so you will have to ignore that in your results.  

You can set a command line to test from the project properties, debug screen.


0
 
Arthur_WoodCommented:
you will need to change the source code of the program, and allow CommandLine arguments.  Then test to see if the user provided a Command line file name, and if NOT, then prompt for it


Here is an idea, using a Console app:

    Public Sub Main(ByVal sArgs() As String)
        'Note the declaration of the Sub Main line
        'It has the sArgs parameter.  This parameter is handled by
        'the system, and contains any command line arguments.

        If sArgs.Length = 0 Then                'If there are no arguments
            Console.WriteLine("Hello World! <-no arguments passed->") 'Just output Hello World
        Else                                    'We have some arguments            
            Dim i As Integer = 0

            While i < sArgs..Length             'So with each argument
                Console.WriteLine("Hello " & sArgs(i) & "!") 'Print out each item
                i = i + 1                       'Increment to the next argument
            End While

        End If

    End Sub


here ia another, using a WinForms app:

Module StartupModule
    Public Function Main(ByVal CmdArgs() As String) As _
        Integer
        Dim frm As New Form1()
        Dim i As Integer

        frm.Text = UBound(CmdArgs) + 1 & " arguments"
        For i = 0 To UBound(CmdArgs)
            frm.lstCommands.Items.Add(CmdArgs(i))
        Next i
        frm.ShowDialog()

        Return 0
    End Function
End Module

you can also check out this othe EE question:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21421584.html

AW


AW
0
 
clebo99Author Commented:
So, how would this replace my:

 OpenFileDialog1.ShowDialog()
 loc = OpenFileDialog1.FileName

Could I just have it as:
arg As String In Environment.GetCommandLineArgs
loc = arg
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
clebo99Author Commented:
So, how would this replace my:

 OpenFileDialog1.ShowDialog()
 loc = OpenFileDialog1.FileName

Could I just have it as:
arg As String In Environment.GetCommandLineArgs
loc = arg

<Added>

Assuming that all I'm going to have trailing the EXE is the path of the file to open?
0
 
clebo99Author Commented:
Also, I put quotes just to designate the file name.  I don't need quotes.  It can be as easy as:

c:\temp\exe.exe file.txt

0
 
PaulHewsCommented:
>So, how would this replace my:

 OpenFileDialog1.ShowDialog()
 loc = OpenFileDialog1.FileName<

I assume that's in a button somewhere?  Put the code to launch the process on the file in a sub.  Then you can do this:


Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Args() As String = Environment.GetCommandLineArgs
        If args.GetUpperBound(0) > 0 Then
            If System.IO.File.Exists(Args(1)) Then
                ProcessFile(Args(1))
            End If
        End If
 
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim loc As String
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            loc = OpenFileDialog1.FileName
            ProcessFile(loc)
        End If
 
    End Sub
 
    Private Sub ProcessFile(ByVal Path As String)
        'code to process and load text box.
    End Sub
End Class

Open in new window

0
 
clebo99Author Commented:
Actually, the opendialog is in the form load so it comes up first, then the form.   I see how you are doing it (and I like how you even put some fault tolerance in it).  I think we have a winner....thanks!!!!
0
 
clebo99Author Commented:
Very quick response.....excellent...
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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