2009-06-15 32 views
0

前把日期我有以下代碼:在XML文件名

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

我試圖把一個日期/時間戳記的XML文件名前面。因此,使用這個例子,我會喜歡0615_Tester.xml

任何想法如何做到這一點?我想確保我可以做日期/時間__ [文件名] .xml,並仍然指定它在我的G:\

在此先感謝。

+0

幾乎相同:http://stackoverflow.com/questions/941132/creating-a-new-txt-file-with-date-in-front-c – crashmstr 2009-06-15 19:16:32

+0

是的,有點......但我試過在這裏,它沒有工作。 – yeahumok 2009-06-15 19:21:11

回答

0

使用的String.format - 例如:

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

... = new StreamWriter(fileName); 

營建 「dateString」 應該從小事DateTime.Now。

0
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); 
0

嘗試是這樣的:

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

我假設該文件已經存在。所以你將不得不復制文件並刪除舊文件。就像這樣:

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); 

您可以添加使用時參考,以保持打字下來,或者在格式化你想要的任何方式日期:

File.Copy(OldFileName, NewFileNameWithDate); 

File.Delete(OldFileName); 
1

這是一個簡單System.IO.Path實現只要它是一個字符串。使用DirectorySeperator變量是reccommended,儘管你可能在Windows編程如果使用.NET(單聲道?)