C # - get file path from configuration file - @ does not do this magic

I am currently working on a web service that retrieves an XML message, archives it, and then processes it further. The archive folder is read from Web.config. Here's 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 structure of the path I get is C: \ Program Files \ Subfolder \ Subfolder. In development environments, QA, UAT and PRD everything works fine. But on another computer, I now need to install the web service (unfortunately, I can’t debug it), the directory line is "C: Files". Just to make sure that I double-checked the .NET version on different machines (I thought maybe using @ before the line depended on the version); all machines use 2.0.50727.

Does anyone know this problem?

Thanks in advance!

EDIT: @ , , . , @( , ).

(): @ , @ "c:\folder\subfolder", , escape-, ? , , ? ( , Path.Combine, . , )

+3
4

Path.Combine() String.Format(). .

+7

, . "@" "" - , . :

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

'@' , . , :

public static void Foo(object @class)
{
     //@class exists here, even though class is a reserved keyword!
} 

, , , "C: ", , "\". 'C:\Files' .

0

Path.Combine(), :

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

, :

@directory

, :

@"c:\myfolder\"

The difference is that the first example allows you to use a reserved word as the name of a variable, for example @class (not used to using it), and the second example allows you to contain strings without characters, for example.

0
source

Source: https://habr.com/ru/post/1743008/


All Articles