How can I make my program find a text file in different directories?

  • Thread starter Thread starter CodeMonkey
  • Start date Start date
AI Thread Summary
To ensure a C# program can access a text file regardless of its location, it's recommended to use a relative path instead of an absolute path. By specifying just "input.txt," the program will search for the file in its current directory, making it portable across different systems. Alternatively, .NET offers a feature called "embedded resources," which allows files to be included within the executable. However, while this method guarantees file availability, it complicates direct manipulation of the input data outside the application, making it less ideal for user-editable input.
CodeMonkey
Messages
11
Reaction score
0
Hi all, I've had great time here with some very helpful answer. Thank you all.

Now my question. I am making a program which takes input from a text file and does somethings with it. Now the directory that text file is on my computer is "C:\input.txt".

but sometimes I want to transport my program and the input.txt file using a thumbdrive or simply transfer it to another computer. When I do I notice the address which I assign the file to be in my C# program (eg.Filestream( @"C:\input.txt", ... , ...) ) is set to C:\.. and since the text file is not there in another computer I am using, my program does not execute.

Is there a way to write the address so that it changes as I move my text file or have the program find the needed text file without it having it to be the "exact" directory?

Thanks.
 
Technology news on Phys.org
The easiest thing to do here is to say not "C:\input.txt", but just "input.txt". If you say just "input.txt" it will look for input.txt in "the current folder".

If you want to be super fancy, there is this trick in .NET where you can actually embed a file "into" your program, so that it is hidden inside of the .exe or whatever, and you can always be guaranteed you will be able to find it. I have never actually used it myself but I think this feature is called "embedded resources" and some information on this seems to show up if you google for
".net assembly embedded resource"
 
But resource files are usually compressed in a .resx file or in an assembly and can't easily be manipulated outside of the app, so it's not the best option for acquiring input data.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top