gpt4 book ai didi

c# - 如何在 Windows 窗体应用程序中访问文本文件

转载 作者:可可西里 更新时间:2023-11-01 09:42:12 24 4
gpt4 key购买 nike

我已将一个文本文件添加到 Windows 窗体应用程序中并尝试访问该文件。

这是我的代码..

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

System.String s = Application.StartupPath+"/Licence.txt";
System.IO.FileInfo fi = new System.IO.FileInfo(s);

if (fi.Exists)
{
if (fi.Length == 0)
{
Application.Run(new Form1());
}
}
}

Licence.txt 存在于我的项目文件夹中,但每次 (fi.Exists) 仅变为 false,并且程序退出流程而不显示 Form1() 窗口。请帮我。提前致谢..

最佳答案

使用这个:

string s = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"Licence.txt");

不要尝试手动组合路径,因为您必须处理路径定界符...使用内置函数要好得多!

最后记得在项目中的 License.txt 文件上设置 Copy to Output 属性。

关于c# - 如何在 Windows 窗体应用程序中访问文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20467815/

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