C# - 从配置文件中检索文件路径 - @ 并没有什么魔力
我目前正在开发一个 Web 服务,该服务检索 XML 消息,将其存档,然后进一步处理它。存档文件夹是从 Web.config 中读取的。这就是存档方法的样子
private void Archive(System.Xml.XmlDocument xmlDocument)
{
try
{
string directory = System.Configuration.ConfigurationManager.AppSettings.Get("ArchivePath");
ParseMessage(xmlDocument);
directory = string.Format(@"{0}\{1}\{2}", directory, _senderService, DateTime.Now.ToString("MMMyyyy"));
System.IO.Directory.CreateDirectory(directory);
string Id = _messageID;
string senderService = _senderService;
xmlDocument.Save(directory + @"\" + DateTime.Now.ToString("yyyyMMdd_") + Id + "_" + System.Guid.NewGuid().ToString().Substring(0, 13) + ".xml");
}
我检索的路径结构是 C:\Program Files\Subfolder\Subfolder。在开发、QA、UAT 和 PRD 环境中一切正常。但在另一台机器上,我现在需要安装 Web 服务(不幸的是,我无法调试),目录字符串是“C:Files”。 为了确保我在不同的机器上仔细检查了 .NET 版本(我认为在字符串之前使用 @ 可能与版本相关);所有机器都使用 2.0.50727。
有人认识到这个问题吗?
提前致谢!
编辑:我在目录变量之前看到@导致了我提出的问题的一些混乱。这与那个@无关(事实上,那不应该在那里。我已经删除了它)。
我的问题(改写)是: 当您在带引号的字符串之前放置 @ 时,例如 @"c:\folder\subfolder" ,它可以确保反斜杠不会被解释为转义字符,对吧?它在一台机器上工作但在另一台机器上不工作可能是什么原因? (我确实同意顺便说一下使用 Path.Combine 的答案。我只是好奇是什么导致了这种不一致的行为)
I'm currently working on a web service that retrieves an XML message, archives it and then processes it further. The archive folder is read from the Web.config. This is what the archive method looks like
private void Archive(System.Xml.XmlDocument xmlDocument)
{
try
{
string directory = System.Configuration.ConfigurationManager.AppSettings.Get("ArchivePath");
ParseMessage(xmlDocument);
directory = string.Format(@"{0}\{1}\{2}", directory, _senderService, DateTime.Now.ToString("MMMyyyy"));
System.IO.Directory.CreateDirectory(directory);
string Id = _messageID;
string senderService = _senderService;
xmlDocument.Save(directory + @"\" + DateTime.Now.ToString("yyyyMMdd_") + Id + "_" + System.Guid.NewGuid().ToString().Substring(0, 13) + ".xml");
}
The path structure I retrieve is C:\Program Files\Subfolder\Subfolder. In the development, QA, UAT and PRD environments everything works fine. But on another machine I now need to install the web service on (which I cannot debug, unfortunately), the directory string is 'C:Files'.
Just to be sure I double checked the .NET version on the different machines (I thought perhaps the usage of @ before a string was version-dependent); all machines use 2.0.50727.
Does anyone recognize this problem?
Thanks in advance!
EDIT: I see the @ before the directory variable has caused some confusion regarding the question I asked. It was not about that @ (in fact, that should not have been there. I have removed it).
My question (rephrased) is:
when you place an @ before a quoted string, like @"c:\folder\subfolder", it ensures that the backslashes are not interpreted as escape characters, right? What could be the cause of it working on one machine, but not working on another?
(I do agree with the answers stating to use Path.Combine by the way. I'm just curious what causes this inconsistent behaviour)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使用 Path.Combine() 而不是 String.Format()。一个很好的示例就在这里。
You could try using Path.Combine() instead of String.Format(). A good example is here.
当从配置文件中提取值时,它会自动正确转义。目录变量名称上的“@”符号并未将其设置为“显式” - 它告诉编译器它是一个命名参数。例如:
变量名上的“@”运算符意味着不将该符号视为保留字。它允许您使用与关键字相同的名称来创建变量名称:
此外,如果它获取的值是“C:Files”,则该值无效,因为它缺少“\”。 “C:\Files”将是有效的。
When a value is pulled from the configuration file, it is automatically escaped properly. The '@' symbol on your directory variable name is not setting it to be 'explicit' - it's tell the compiler that it is a named parameter. For example:
The '@' operator on a variable name means to not treat that symbol as a reserved word. It allows you to make variable names with the same name as a keyword:
In addition, if the value it is getting is 'C:Files', then that is invalid, as it is missing a '\'. 'C:\Files' would be valid.
使用 Path.Combine(),例如:
Use Path.Combine(), for instance:
从你的问题来看,我认为你已经处理过:
就好像它执行了与以下相同的功能:
区别在于第一个示例允许您使用保留字作为变量名,例如 @class (不要养成习惯使用它),第二个示例允许字符串包含未转义的字符,例如 .
From your question I think that you've treated:
As if it performed the same function as:
The difference is that the first example allows you to use a reserved word as a variable name, like @class (don't get into the habit of using it) and the second example allows the string to contain unescaped characters such as .