Good time of day, community! Tell me how to add a variable to the path when using the WriteXML method? I want it to be: string name = DateTime.Today.ToString (); sRDataSet.WriteXml ("C: /sanbak/name.xml");

  • sRDataSet.WriteXml("C:/sanbak/" + name + ".xml"); ? - Alexey Shimansky
  • Unfortunately not, with this format an exception occurs, which tells us that: This path format is not supported, this option was the first thing that came to mind. - Madushko
  • Bring the date in the correct form in the same ToString in a format acceptable to you - Alexey Shimansky
  • Showed in the answer how you can format the date. - Alexey Shimansky

1 answer 1

Method WriteXML , in principle, nothing to do with. The path is transmitted in the form of a string, and in strings it is possible to make concatenation, including with variables:

 string myValue = "Hello, world"; Console.WriteLine("Текст из переменно myValue: " + myValue); 

Accordingly, the line will be in your case:

 string name = DateTime.Today.ToString(); sRDataSet.WriteXml("C:/sanbak/" + name + ".xml"); 

Just do not forget to format the date in the form that interests you. Because it is raw, i.e. when writing DateTime.Today.ToString(); or DateTime.Now.ToString(); etc. - the date will be 6/18/2016 5:07:03 PM/AM . As you can see, it will have slashes, as well as colons, which are not allowed in the file name. Because of this, errors will appear that this format of the path is not supported .

So you need to force to lead to the correct format.

For example:

 string name = DateTime.Today.ToString("d-MM-yyyy"); 

So that was the format day-month-year

Or more difficult

 string name = DateTime.Now.ToString("dd.MM.yyyy HH'h'mm'm'ss's'"); 

It will display something like: 19.06.2016 10h23m38s


What formats are there - you can watch directly on MSDN, for example

https://msdn.microsoft.com/ru-ru/library/zdtaw1bw(v=vs.110).aspx

https://msdn.microsoft.com/ru-ru/library/az4se3k1(v=vs.110).aspx

Or where else

  • I wonder if we add formatting when everything works, and if you just use 'DateTime.Today.ToString (); 'swears on the format. Thank! Works! - Madushko
  • @Madushko Because the date is raw 6/18/2016 5:07:03 PM/AM . Those. will have slashes as well as colons that are not valid in the file name. Because of this, mistakes will fall out - Alexey Shimansky