C#如何在app.config文件中指定appData文件路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/964132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# how to specify the appData file path in the app.config file
提问by ant2009
I am using log4net and I was to save the log file in the AppData file for win XP/Vista etc.
我正在使用 log4net 并且我要将日志文件保存在 AppData 文件中以用于 win XP/Vista 等。
This is my app.config file so far, and I have specified the name softphone.log. Hoewver, I am not sure how to specify the complete path to the file as each user will have a different path depending on their username.
到目前为止,这是我的 app.config 文件,我已经指定了名称 softphone.log。Hoewver,我不确定如何指定文件的完整路径,因为每个用户将根据其用户名拥有不同的路径。
<log4net>
<logger name="default">
<level value="DEBUG"/>
</logger>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="softphone.log"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackup value="10"/>
<maximumFileSize value="1MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
</appender>
</log4net>
In my source code I can get the path by doing the following:
在我的源代码中,我可以通过执行以下操作来获取路径:
System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
However, I can't use the above in my app.config and if I hard coded the path on my system the path would be:
但是,我不能在我的 app.config 中使用上述内容,如果我在系统上硬编码路径,路径将是:
C:\Documents and Settings\John\Application Data
However, this would be different for each client. So is there a way to do this for the app.config file?
但是,这对于每个客户来说都是不同的。那么有没有办法为 app.config 文件做到这一点?
Many thanks for any suggestions,
非常感谢您的任何建议,
采纳答案by Martin Harris
I don't believe that you can do what you want, there is a method for custom parsing areas of the app.config file so that you could add your own token that you could replace with the correct value, but I don't see how that would work inside the log4net section.
我不相信你可以做你想做的事,有一种方法可以自定义解析 app.config 文件的区域,这样你就可以添加自己的令牌,你可以用正确的值替换它,但我没有看到这将如何在 log4net 部分中工作。
However, everything that is set up for log4net inside the config can also be set in code. I think you're best option would be to set the property for the appender in code just after application start.
但是,在配置中为 log4net 设置的所有内容也可以在代码中设置。我认为您最好的选择是在应用程序启动后立即在代码中设置附加程序的属性。
Ahh, never mind a quick search has revealed my ignorance. From hereand hereit appears that something similar to this:
啊,没关系快速搜索暴露了我的无知。从这里和这里似乎有类似的东西:
<file value="${APPDATA}\log-file.txt" />
Will do what you want. I haven't tested this myself, so I'll leave my first answer up too - but I'd be interested to know if you have any luck with it.
会做你想做的。我自己还没有测试过这个,所以我也会留下我的第一个答案 - 但我很想知道你是否有运气。
回答by Vivek Raj
User %appdata%. So your code will look like -
用户 %appdata%。所以你的代码看起来像 -
<file value="%appdata%/softphone.log"/>