My.Application.CommandLineArgs in C#

Nieminen247

Newcomer
Joined
Jun 21, 2008
Messages
4
In VB.NET there is the My.Application.CommandLineArgs, but is there any similar class in C# that will watch, what file and where from the application is started ???

I need to compile this to C#

Code:
Dim Parameter As String
        If My.Application.CommandLineArgs.Count > 0 Then
            For Each Parameter In My.Application.CommandLineArgs
                MsgBox(Parameter)
            Next
        End If
 
Thanks!
But now i need help how to adjust that to retrieve only the location and the filename of the file that started the application..

For example : i would like to get my text editor to load text to the textbox if the application is started by some .txt file (open with --> My own text editor)

i just need the code example that shows the location and the filename of the .txt file , it is the easy part then to load the text from that file to textbox . :D
Something like :
String path = Environment.GetCommandLineArgs();
Richtextbox1.text = path;

I just dont think that it is so easy ;
 
The GetCommandLineArgs returns an array containing all the arguments passed on the command line, if the file to open is the first one then it will be at position 0 in the returned array.

Easiest way to get the information into the text box is by using a StreamReader object to open the file.
 
i can now retrieve the filename like that

Code:
String path = System.Environment.CommandLine;
            if (path.IndexOf(Application.ExecutablePath) != -1)
            {
            label1.Text = path.Replace("\"" + Application.ExecutablePath + "\"", "");
            }

but is there any other way / easier way to do it?:eek: :eek:
 
That code, what i posted will show the filename that opened the application,
So if i have a text-editor and i want that it loads the text to the textbox when it is started(Onload) BY .txt file(hello.txt --> rightclick --> open with --> my own text editor).

that is just a example to test that it works when i put the text to the label.

So heres what the code does

IF path contains applications executable path (location+filename)
THEN
replace the " & executable path & " with " "

so that it will only show the text filename and location that opened the application


if the application is opened without any .txt files then it will not load text to textbox

(I am trying to make own text editor like notepad, and when i doubleclick .txt file it will open it to my own editor and load the text to there)
i can do everything else of that text editor with myself, but it is not handy to open .txt files through the openfiledialog) :D
 
Hi. Not sure if you figured it out yet, but perhaps the code below accomplished what you are trying to do?
Code:
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length > 1)
            {
                string FileToLoad = args[1];
                string BasePath = Application.ExecutablePath;
                if (!BasePath.EndsWith("\\")) BasePath += "\\";
                //If FileToLoad is the name of a textfile, just append it to the base path
                string FileToLoad = BasePath + FileToLoad;
                //do stuff to load the file into the textbox
            }
 
Back
Top