我有一个.csv文件,我已经包含在我的项目中。我创建了一个StreamReader,它将路径作为参数,但我必须包含文件的完整路径,而不仅仅是文件名。
即。 new StreamReader("products.csv");
代替
new StreamReader(@"C:\Users\user\Documents\Visual Studio 2015\Projects\Solution\Project\Products\products.csv");
如何使StreamReader只接受文件名而不是整个路径?
答案 0 :(得分:2)
您可以将文件移动到bin \ debug文件夹中并使用:
new StreamReader("products.csv");
答案 1 :(得分:2)
您可以添加配置
的根路径public class StreamReader
{
private string _filePath;
public StreamReader(string filePath)
{
_filePath = Path.IsPathRooted(filePath)
? filePath
: Path.Combine(System.Configuration.ConfigurationManager.AppSettings["CsvRootPath"], filePath);
}
}