tags:

views:

82

answers:

5

I have the following code:

XmlSerializer SerializeObj = new XmlSerializer(dirs.GetType());  
TextWriter WriteFileStream = new StreamWriter(@"G:\project\tester.xml");  
SerializeObj.Serialize(WriteFileStream, dirs);  
WriteFileStream.Close();

I'm trying to put a date/time stamp in FRONT of the xml file name. Therefore, using this example, i'd have something like 0615_Tester.xml

Any ideas on how to do this? I want to make sure i can do date/time__[filename].xml and still specify it to be on my G:\

Thanks in advance.

A: 
string yourDateString = DateTime.Now.ToString(); // replace with any way you want to get your date string    
string filename = "G:\\project\\" + yourDateString + "_tester.xml";
TextWriter WriteFileStream = new StreamWriter(filename);
McWafflestix
this produces an xml file called tester.xml as i had before.
A: 

Try something like this:

string filePath = string.Format("G:\\project\\{0}tester.xml", Date.Now.ToString("DDMM"));
TextWriter WriteFileStream = new StreamWriter(filePath);
Oded
A: 

I'm assuming that the file already exists. So you will have to copy the file and delete the old one. Like this:

File.Copy(OldFileName, NewFileNameWithDate);

File.Delete(OldFileName);
roosteronacid
+1  A: 

This is Simply achieved with System.IO.Path:

string path = "G:\\projects\\TestFile.xml";
string NewPath = System.IO.Path.GetDirectoryName(path) + 
    System.IO.Path.DirectorySeperatorChar + 
    DateTime.Now.ToString() + 
    System.IO.Path.GetFileName(path);

You can add a using Reference to keep the Typing down, or Format the Date in any way you want as long as its a string. Using the DirectorySeperator Variable is reccommended, although you are probably programming for Windows If using .NET (Mono?)

wsd
A: 

Use string.Format - for example:

string dateString = "0615";
string fileName = string.Format(@"G:\project\{0}_tester.xml", dateString);

... = new StreamWriter(fileName);

Building up "dateString" should be trivial from DateTime.Now.

Bubbafat