C# Console app. file relative path problem

jonatec
jonatec used Ask the Experts™
on
With a C# console app. I want to read in text files held in a sub-folder call "/scripts". Obvious I don't want to hard-code a physical path. Is there a way to make relative reference to the file path? Or is there a better way to do this ?

Thanks.
string filepath = "scripts\\" + p_sproc_name + ".sql";
      TextReader tr = new StreamReader(filepath);

Open in new window

dump.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Create a DirectoryInfo object and get all files in it with .sql extension.

DirectoryInfo di = new DirectoryInfo(@"C:\temp");
            FileInfo[] files = di.GetFiles("*.sql");
            foreach (FileInfo fi in files)
            { }

Open in new window

Also you can keep the scripts folder path in the  app.config if required.
You can also search for a specific file by giving the name the search pattern.
try this:

string filepath = Environment.CurrentDirectory+"\\scripts\\" + p_sproc_name + ".sql";
      TextReader tr = new StreamReader(filepath);

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
guptavipin:

Thanks, the DirectoryInfo tip is good, but there is still a physical path used.

muhammadyasir:

Tried Environment.CurrentDirectory, but this seems to point to ..bin/Debug

Q.

Are you guys basically saying there is no way to create a relative path, as would be the case in a WebForm App?


Current directory will give you the path of the directory where the executable is.
The best approach is to keep the path in the app.config file and read it using the configuration manger. If the Scripts folder is in the same location as exe  then you can just use the CurrentDirectory + Folder name from app.config .
If scripts folder can be anywhere like C:\temp\Scripts then place the complete path in app.config and readt it to create the DirectoryInfo Object.
Top Expert 2010

Commented:

In Project -> properties -> Build events, add the following:

xcopy "$(ProjectDir)scripts\*.*" "$(TargetDir)scripts\" /Y

This will copy the scripts folder to your output folder where you .exe is built to.

Your original code will now work with this as scripts folder is now in current directory of the app.
      string filepath = "scripts\\" + p_sproc_name + ".sql";
      TextReader tr = new StreamReader(filepath);

Top Expert 2010

Commented:

When deploying your project to other locations, you would copy the whole contents in the bin/debug folder which now will include scripts folder as well..

Author

Commented:
Yeah, I like the use of DirectoryInfo to trawl the file list and to use the App.config for any paths. Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial