Nieminen247 Posted June 21, 2008 Posted June 21, 2008 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# Dim Parameter As String If My.Application.CommandLineArgs.Count > 0 Then For Each Parameter In My.Application.CommandLineArgs MsgBox(Parameter) Next End If Quote
Administrators PlausiblyDamp Posted June 21, 2008 Administrators Posted June 21, 2008 Environment.GetCommandLineArgs() will return the command line arguments regardless of the language. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nieminen247 Posted June 21, 2008 Author Posted June 21, 2008 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 ; Quote
Administrators PlausiblyDamp Posted June 22, 2008 Administrators Posted June 22, 2008 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nieminen247 Posted June 22, 2008 Author Posted June 22, 2008 i can now retrieve the filename like that 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: Quote
Administrators PlausiblyDamp Posted June 22, 2008 Administrators Posted June 22, 2008 Not sure what you are trying to do with the code you posted... Are you trying to pass a filename as an argument to the application? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nieminen247 Posted June 23, 2008 Author Posted June 23, 2008 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 Quote
stumper66 Posted July 4, 2008 Posted July 4, 2008 Hi. Not sure if you figured it out yet, but perhaps the code below accomplished what you are trying to do? 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 } [/Code] Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.