Resolved Finding .txt file

bob1231

Member
Joined
Mar 8, 2021
Messages
8
Programming Experience
Beginner

I have a common problem. How to find and open .txt file in C# on any computer? Normally I use: "Process.Start(file localization);" , but if someone install my program there wouldn't have a same file path. Is there way to achieve the installation folder with .txt file?

 
I think you need to provide a little more detail and precision to that statement where you said that you normally use Process.Start(file localization);.

Did you mean Process.Start(file, localization); where file is a string which contains the your program's executable, and localization is a string which contains the name of the .txt file that your application will open?

Or did you mean Process.Start(fileLocalization); where file localization is a string which contains the name of the .txt file that your application will open?

Or did you mean Process.Start("file localization.txt"); ?

In general, the current working directory of an application is set to be the same directory that the executable is found in. So as long as the text file you are trying to open is in installed in the same directory as your executable, then you should be able to find it. A user has deliberately go out of their way to create a desktop or taskbar shortcut to your application and change the working directory to be different from the location of your application.

Also in the case last two cases above were the is only one parameter passed to Process.Start(), the OS takes care of looking through the registry to determine what application is associated with the file extension of the filename passed in. So in the last two cases above, whatever is associated with .TXT will be the application that is launched. So it doesn't really matter as much as where your application is installed, but it matters more of where the .TXT file is placed and whether the associated application will know how to find it. In general, it's a good idea to pass a fully qualified path to the .TXT file, which I guess brings us back full circle to determining where your application was installed.

To determine where your application was launched from, most of the time Assembly.GetEntryAssembly() or Application.StartUpPath will give you the information that you need.
 
Back
Top Bottom