我在Visual Studio中从WPF构建可执行应用程序,并且我在项目中添加了一个本地文本文件(StringList.txt),我试图将其读入List对象。对于StreamReader,该文件的路径是什么,以便可执行文件在部署并在另一台计算机上运行时可以访问该文件?
到目前为止,我有:
List<String> list = new List<String>();
StreamReader sr = new StreamReader("~/Desktop/Deploy/StringList.txt");
String line = sr.ReadLine();
while (line != null)
{
fields.Add(line);
line = sr.ReadLine();
}
sr.Close();
** Deploy是发布文件夹位置
答案 0 :(得分:1)
~/Desktop/Deploy/StringList.txt
不适用于WPF ..
试试这个:
// Get the directory where your entry assembly is located (your application)
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// Add your sub directory and filename
var filename = assemblyPath + "\\Desktop\\Deploy\\StringList.txt";
// Use it on your StreamReader
StreamReader sr = new StreamReader(filename);
这将在程序的子目录中打开文件。