-1

If my directories look like this:

MyProject
 MyProject
  some_dir
   file_I_wanna_read
  here
   Here.cs

How do I read file_I_wanna_read from Here.cs, without having to type the whole path? (eg. "C:\my_user\Projects\MyProject\MyProject\some_dir\file_I_wanna_read")

I've already tried writing the path as if I were at the solution:

MyProject\some_dir\file_I_wanna_read

And also as if I were in the .csproj:

some_dir\file_I_wanna_read

But those don't work.

  • 2
    Using relative file paths in a program entirely depends on the Current Working Directory when the program runs. – gunr2171 Aug 15 '21 at 18:12
  • 1
    Does this answer your question? [Relative path to absolute path in C#?](https://stackoverflow.com/questions/4796254/relative-path-to-absolute-path-in-c) and [Converting absolute path to relative path C#](https://stackoverflow.com/questions/13019402/converting-absolute-path-to-relative-path-c-sharp) –  Aug 15 '21 at 18:12
  • 3
    Relative pathing rules say that `..` is one step up the pathing heirarchy. So `..\some_dir\file_I_wanna_read` should do the trick. Asuming the right working directory. – Christopher Aug 15 '21 at 18:16
  • Your program doesn't read from folders relative to where the `.cs` file is, it reads from folders relative to the current directory is (something over which you have little control). You can find out the folder in which you program is running, and use it as a reference. In addition, if you open the properties of the `file_I...` file and change them to `Content` and `Copy Always`, then your file will be copied to your assembly's folder (or a sub folder under that). Then you can compose an absolute path to the file – Flydog57 Aug 15 '21 at 18:49

2 Answers2

0

Here's a solution.

It relies on getting the FileYouWantToRead copied into the EXE's location where it's findable.

  1. Create a simple, out-of-the-box WinForms app.
  2. Right-click the project, select Add then New Folder, name the folder "SomeDir"
  3. Right-click the new "SomeDir" folder and select Add and then New Item
  4. In the dialog that pops up, search for "Text", pick Text File and name the file "FileIWantToRead.txt"
  5. When the file opens, type "This is the file I want to read" and save the file
  6. Right-click the new "FileIWantToRead.txt" file and choose properties
  7. In the properties pane, set Build Action to Content and Copy to Output Directory to Copy Always
  8. Go back to the form designer, drop a button and a label control on the form
  9. Double-click the button
  10. In the "Form1.cs" button click handler that opens, put this code:

Form1.cs button click handler:

private void button1_Click(object sender, EventArgs e)
{
    var fullExeName = Assembly.GetExecutingAssembly().CodeBase;
    if (fullExeName.StartsWith("file://"))
    {
        fullExeName = fullExeName.Substring(8);
    }
    var exeDir = Path.GetDirectoryName(fullExeName);
    var fileName = Path.Combine(exeDir, "SomeDir", "FileIWantToRead.txt");
    var content = File.ReadAllLines(fileName)?.First();
    label1.Text = content ?? "Not Found";
}

The "Copy Always" action will cause your text file to get copied to the output EXE's folder. In this case, since it's within the "SomeDir" folder, it will get copied into a "SomeDir" folder under the output folder.

I get the EXE's folder from the executing assembly (i.e., the running exe) and use the various utilities in System.IO to get the right folder and file names. After clicking the button, the first line of that file will show in the label.

Flydog57
  • 6,851
  • 2
  • 17
  • 18
-1

First you can get current directory of your program. (Read these official docs for further help)

Then you can access your file with directory related to current directory. Your string of directory accessing should be something like this:

..\some_dir\file_I_wanna_read

(Double dots mean go one directory backward or up.)