jmkjaer
jmkjaer

Reputation: 1098

How to avoid including full path to file

I've got a .csv-file that I've included in my project. I've created a StreamReader that takes a path as parameter, but I have to include the full path to the file instead of just the file name.

I.e. new StreamReader("products.csv"); instead of

new StreamReader(@"C:\Users\user\Documents\Visual Studio 2015\Projects\Solution\Project\Products\products.csv");

What can I do to make the StreamReader accept just a filename instead of a whole path?

Upvotes: 1

Views: 267

Answers (2)

Clint
Clint

Reputation: 2793

You can move the file into your bin\debug folder and use this:

new StreamReader("products.csv");

Upvotes: 2

maxlego
maxlego

Reputation: 4914

You can add root path to config

public class StreamReader
{
    private string _filePath;

    public StreamReader(string filePath)
    {
        _filePath = Path.IsPathRooted(filePath)
            ? filePath
            : Path.Combine(System.Configuration.ConfigurationManager.AppSettings["CsvRootPath"], filePath);
    }
}

Upvotes: 2

Related Questions