开发者

C# - retrieve file path from config file - @ doesn't do it's magic

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 开发者_JS百科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)


You could try using Path.Combine() instead of String.Format(). A good example is here.


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:

public void (string[] args)
{
   int length = args.Length;
   length = @args.Length; // Same thing!
}

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:

public static void Foo(object @class)
{
     //@class exists here, even though class is a reserved 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.


Use Path.Combine(), for instance:

strFilename = CombinePaths(directory, _senderService) + DateTime.Now.ToString("MMMyyyy");


From your question I think that you've treated:

@directory

As if it performed the same function as:

@"c:\myfolder\"

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 .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜