如何在App.Config文件中设置要读取的文件的通用路径
我有一些位于 Visual Studio 项目中的文本文件需要读取。我的 app.config 文件应该自动调整这些文件的路径。即,假设我在位置 C:\Visual Studio\Test Project\Read.txt 中有一个文件,当我放入 D:\ 驱动器时同一个项目,我不应该将路径更改为 D:\Test Project\Read。 txt 它应该自动适应位置的变化。
I have some text files to be read that are located in the visual studio project. My app.config file should adapt the path of these files automatically. i.e., suppose I have a file in the location C:\Visual Studio\Test Project\Read.txt, the same project when I put into D:\ drive, I should not change the path to D:\Test Project\Read.txt It should adapt the change in location automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 app.config 文件无法自动适应。通常,这是安装或部署脚本的任务,以确保所有配置均正确设置。
但是,如果您的程序知道您的配置文件正在存储相对路径,则它可以动态调整。而不是执行
此操作(引用相对于当前正在执行的程序集的文件):
Your app.config file cannot adapt automatically. Generally, this is the task of installation or deployment scripts, to make sure that all the configuration is set up properly.
However, your program can dynamically adapt if it knows that your configuration files is storing relative paths. Instead of doing
Do this (reference the file relative to the currently executing assembly):