c# - 如何使用c#从应用程序的安装文件夹中读取文件?

标签 c# windows windows-services

我正在使用 File.ReadAllText("filename.txt") 读取文件。该文件与 .exe 文件位于同一文件夹中(c:/program files/installation folder)。但是默认情况下,Windows 应用程序会在 System32 文件夹中查找此文件。

**我不想硬编码文件的路径。

最佳答案

试试这个函数:

using System.Reflection;

private string MyDirectory()
    {
        return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    }

然后使用

File.ReadAllText(MyDirectory() + @"\filename.txt");

关于c# - 如何使用c#从应用程序的安装文件夹中读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2730800/

相关文章:

python - 在python中从返回的JSON转换unicode字符

windows - 通过 docker-compose 将 build-args 传递给 Dockerfile 的奇怪行为

iis - Windows 7中IIS的inetmgr.exe的默认位置是什么,因为我的Windows搜索均无法正常进行。有其他选择吗?

configuration - Windows 服务的 app.config 无法即时运行

php - 使用 PHP 在 Windows 服务器上复制文件

c# - 如何以编程方式授予虚拟用户对文件夹(或文件)的权限

c# - 外键组件 'X' 不是类型 'Y' 的声明属性。验证它没有被明确排除

所有其他应用程序上的 C# 气球通知弹出窗口

c# - 在 OpenXML 中处理 Word 文档时从 OpenXmlElement 获取 DocumentPart

c# - FileStream 不允许我在本地计算机上创建文件?