locked
How can i navigate to a folder in a visusual stduio project ? RRS feed

  • Question

  • User1253338400 posted

    Hi ,

    I have a project and in the project I have a folder called  DataFolder and a folder called Data1  and in there is a number of files , How can i get the pathe to that and get all files below using c#

    I have tried using 

    string[] Documents = System.IO.Directory.GetFiles(@"DataFolder /Data1");

    this gives an exception directory not found 

    what am i doing wrong here ?

    thanks

    Wednesday, February 22, 2017 2:39 AM

Answers

  • User-1838255255 posted

    Hi robby32,

    According to your description, please use the following code to read file in root of the project.

    AppDomain.CurrentDomain.BaseDirectory method will get the path of this project.

    App_Data\Names.txt is the file folder and file name.

    Path.Combine method will combine both of paths.

    Code:

     protected void Button1_Click(object sender, EventArgs e)
            {
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\Names.txt");
                string[] files = File.ReadAllLines(path);
            }

    Result:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2017 3:11 AM

All replies

  • User197322208 posted

    First, it is a space here:

    System.IO.Directory.GetFiles(@"DataFolder /Data1");

    Second, does the folder exists in bin/debug ?

    If not, right click any file inside the folder , properties, "Copy to output directory" => true.

    Wednesday, February 22, 2017 5:18 AM
  • User-1838255255 posted

    Hi robby32,

    According to your description, please use the following code to read file in root of the project.

    AppDomain.CurrentDomain.BaseDirectory method will get the path of this project.

    App_Data\Names.txt is the file folder and file name.

    Path.Combine method will combine both of paths.

    Code:

     protected void Button1_Click(object sender, EventArgs e)
            {
                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\Names.txt");
                string[] files = File.ReadAllLines(path);
            }

    Result:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2017 3:11 AM