简体   繁体   中英

Reference txt file in solution folder across multiple projects for homework assignment

Basics of assignment

  1. Use Visual Studio (I'm in 2019, thank you school!)
  2. Create console app that displays Magic 8 Ball style responses randomly to any text entered in line.
    • This list is stored as a .txt file somewhere within the solution
  3. Create windows form app that allows a user to enter new responses that are saved to the list pulled in the console app above
    • This only accepts one line at a time with a listbox displaying the results and a save button for saving. Form has label ("Enter a magic response!"), text box for input, list box for displaying list from .txt file, and save button to add text box entry to .txt file.

I don't know how to reference across projects, and my developer lingo is still too underdeveloped for me to conduct a worthwhile Google search. I simply don't know how to reference a relative path that keeps things contained. I only know how to reference an absolute path:

StreamWriter outputfile;
outputfile = File.AppendText(@"C:\Users\...some-file-path...\Homework2\responses.txt)

Or deeper if it's in the bin\\debug folder. Speaking of, I don't care if it's in the debug folder of one project or another, or in the main solution folder.

I just want it to work since it's homework (although an explanation of common industry best practices would not be unappreciated, even though I'm going into IT). How do I reference the solution folder as the start of the path to which I need to find a file? I would ask if that's possible, but I know the reality of programming is that anything is possible if you're just willing to put enough knuckle grease into the project.

You can use .. to go up a directory relative to the current one. So going from C:\\Windows\\Temp using ..\\..\\ProgramData would get you to C:\\ProgramData .

The current dir is referenced with .\\ for going downwards in the tree. So using the previous example .\\Temp1 would get you to C:\\Windows\\Temp\\Temp1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM