gpt4 book ai didi

c# - 使用相对路径将文件添加到解决方案

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:05 25 4
gpt4 key购买 nike

我在 VS2010 中向我的解决方案添加了一个文本文件,并将其命名为 test.txt。

在文件的属性中,我设置了copy to output:alwaysbuild action:content

我现在如何在我的项目中打开这个文件?因此,如果用户按下按钮,它将打开文本文件。

我已经尝试了几种方法,比如 File.open("test.txt")System.Diagnostics.Process.Start(file path)) 什么都没有已经工作了。

谁能提供一些建议?

最佳答案

由于您使用复制来输出文件,因此您可以将文件放置在与您的程序相同的目录中:

System.Diagnostics.Process.Start("test.txt");

或基于此MSDN article :

string path = "test.txt";
using (FileStream fs = File.Open(path, FileMode.Open))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b, 0, b.Length) > 0)
{
textBox1.Text += (temp.GetString(b));
}
}

关于c# - 使用相对路径将文件添加到解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11130901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com