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

  • Thread starter Thread starter CodeMonkey
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on how to make a C# program locate a text file across different directories, particularly when transferring the program and file via a thumb drive. The recommended solution is to use a relative path, such as "input.txt", which allows the program to search for the file in the current working directory. Additionally, the concept of using embedded resources in .NET is mentioned as a method to include files within the executable, although it is noted that this approach complicates file manipulation.

PREREQUISITES
  • Understanding of C# file handling
  • Familiarity with relative vs. absolute file paths
  • Basic knowledge of .NET embedded resources
  • Experience with file I/O operations in programming
NEXT STEPS
  • Research C# FileStream class for file handling
  • Learn about relative paths in C# applications
  • Explore .NET embedded resources and their implementation
  • Investigate best practices for file I/O in portable applications
USEFUL FOR

C# developers, software engineers, and anyone involved in creating portable applications that require file input handling.

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.
 

Similar threads

Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
Replies
81
Views
7K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K