app.config中的文件路径 [英] FIle path in app.config
问题描述
每当我们运行Windows应用程序时,我总是怀疑路径是如何形成的。
我在我的应用程序配置中设置了这样一个键
< add key = LogFilePath value = .. \\\\\/>
当我从本地机器运行时,它提供了运行Windows应用程序的路径。
但是当我从TFS运行相同的项目时,当我尝试在Log文件夹中创建一个文件,而不是项目映射路径时,它提供了一个完全不同的路径。
谁能告诉我为什么会这样?
我建议你在代码中确定相关的运行时目录路径: pre lang =cs> string appStartUp = Application.StartupPath;
string currentDirectory = Environment.CurrentDirectory;
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);全局系统路径的其他有用信息可以在Environment.SpecialFolder枚举中找到:[ ^ ]。
请注意,日志文件不能放在程序文件夹中:非管理用户不能有足够的特权来写它。使用例如%APPDATA%
。这可以通过Environment类扩展。
< add key = LogFilePath value = %appdata%\YourCompany\Logs\xy.log / >
string val = ConfigurationManager.AppSettings [ LOGFILEPATH跨度>];
string path = System.Environment.ExpandEnvironmentVariables(val);
Hi,
I always have doubt regarding how a path is formed whenever we run a windows app.
I have set a key like this in my app config
<add key="LogFilePath" value="..\Log\" />
When i run this from my local machine, it provides the path from where the windows app is run.
But when i run the same project from TFS, and when i try to create a file inside the Log folder , instead of the project mapped path it gives an entirely different path.
Can anyone tell me why this happens?
I suggest you determine the relevant run-time directory-paths in code:string appStartUp = Application.StartupPath; string currentDirectory = Environment.CurrentDirectory; string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);Other useful information for global system paths can be found in the Environment.SpecialFolder Enumeration: [^].
Note that a log file must not be placed in the program folder: a non-administrative user does not have sufficient priviledges to write it. Use e.g.%appdata%
. That can be expanded by the Environment class.
<add key="LogFilePath" value="%appdata%\YourCompany\Logs\xy.log" />
string val = ConfigurationManager.AppSettings["LogFilePath"]; string path = System.Environment.ExpandEnvironmentVariables(val);
这篇关于app.config中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!