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

标签 c# io windows-forms-designer file-handling windows-applications

我已将一个文本文件添加到 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/

相关文章:

c# - 从 c# 中的 list<object> 列表中删除重复的内部列表

csv - 去CSV文件解析

java - 读入一个没有标点符号的文件

linux - 使用 select() 进行 I/O 多路复用

winforms - Winforms Designer 如何实例化我的表单?

c# - 容器 UserControl - 处理和修改添加的控件

javascript - 如果 HTML 包含空格,JQuery HTML 未终止字符串文字错误

c# - 异步 lambda : "a task was cancelled"

c# - 设计器中的 ContextMenuStrip 事件

c# - 当一个域 Controller 关闭时,WCF Kerberos SSPI 失败