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

  • Thread starter CodeMonkey
  • Start date
In summary, the conversation discusses the issue of transporting a program and its input file to another computer. The solution proposed is to use relative file paths, such as "input.txt", instead of absolute paths like "C:\input.txt". Another option mentioned is embedding the file into the program, but it may not be the best option for acquiring input data.
  • #1
CodeMonkey
11
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
  • #2
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"
 
  • #3
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.
 

1. What is the importance of addresses in scientific research?

Addresses are important in scientific research as they provide a specific location for where the research was conducted. This is important for replicating the study and understanding any potential environmental influences on the results.

2. How are addresses collected in scientific studies?

Addresses can be collected through a variety of methods, such as surveys, interviews, or data collection from public records. Researchers may also use GPS technology to pinpoint the exact location of a study.

3. Can addresses be used to protect the privacy of participants in a study?

Yes, addresses can be used to protect the privacy of participants by using a coding system to replace the actual address. This allows researchers to still analyze data by location without compromising the privacy of participants.

4. Are there any limitations to using addresses in scientific research?

One limitation is that addresses may change over time, making it difficult to track participants for long-term studies. Additionally, not all participants may have a fixed address, which can create bias in the study results.

5. How can addresses be used in data analysis?

Addresses can be used in data analysis to create maps and visualize patterns or trends in research findings. They can also be used for geospatial analysis to understand the relationship between variables and their location.

Similar threads

  • Programming and Computer Science
Replies
3
Views
381
  • Programming and Computer Science
Replies
1
Views
252
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
9K
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
22
Views
919
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top