2013-07-22 27 views
1

林創建一個文本文件,最後一行是「」如何防止添加一個空行到我的文件?

private void lastRunDate() 
    { 
     String lastLine = readLastDate(); 
     String[] date = lastLine.Split('/'); 
     DateTime dt = new DateTime(Int32.Parse(date[2]), Int32.Parse(date[0]), Int32.Parse(date[1])); 
     DateTime currentDT = DateTime.Now; 

     argValue = 1; 

     if ((dt.Month == currentDT.Month) && (argValue == 0)) 
     { 
      MessageBox.Show("This application has already been run this month"); 
      this.Close();     
     } 
    } 


    private void AddRecordToFile() 
    { 
     DateTime now = DateTime.Now; 
     prepareToEmail(); 
     string path = filepath; 
     bool dirtyData = true; 
     // This text is added only once to the file. 
     if (!File.Exists(path)) 
     { 
      // Create a file to write to. 
      using (StreamWriter sw = File.CreateText(path)) 
      { 
       sw.Write(now.ToShortDateString());      
      } 
      dirtyData = false; 
     } 

     if (dirtyData) 
     { 
      // This text is always added, making the file longer over time 
      // if it is not deleted. 
      using (StreamWriter sw = File.AppendText(path)) 
      { 
       sw.Write(now.ToShortDateString()); 
      } 
     }    
    } 

    private String readLastDate() 
    { 
     using (StreamReader sr = new StreamReader(filepath)) 
     { 
      // Initialize to null so we are not stuck in loop forever in case there is nothing in the file to read 
      String line = null; 
      do 
      { 
       line = sr.ReadLine(); 
       // Is this the end of the file? 
       if (line == null) 
       { 
        // Yes, so bail out of loop 
        return "01/01/1900"; // I had to put something 
       } 
       // Is the line empty? 
       if (line == String.Empty) 
       { 
        // Yes, so skip it 
        continue; 
       } 
       // Here you process the non-empty line 
       return line; 
      } while (true); 
     } 
    } 

是我使用創建文件(或追加吧)

現在是一個DateTime對象

我用什麼你的(卡爾)代碼來創建一個名爲「readLastDate()」的方法。

我得到的是第一個日期。

+4

使用'Write',不'WriteLine'。 'WriteLine'附加一個新的行字符。 –

+0

如果我這樣做,那麼所有的數據都在1行。我需要每一行沒有日期 –

+0

請不要LINQ。我正在使用.Net 2.0 –

回答

1

你可以使用一個sw.Write和PRE-掛起一個換行符。不幸的是,這會在文件開始時給你一個空行。

+0

不能預先支持該文件。這些文件在應用程序運行之前不存在 –

+0

'if(!isFirstLine)prepend' ... – Stormenet

+0

@CocoaDev我並不是說要在文件前添加文件,而是在日期前添加一個Environment.NewLine。無論如何,你的代碼中都會跳過空行。 – Richard

1

這樣做:

sw.Write(now.ToShortDateString()); 

這裏是StreamWriter.WriteLine MSDN文檔。

以下是StreamWriter.Write的MSDN文檔。

UPDATE:

,繼續使用WriteLine,但改變你從文件中讀取的值的方式:

using (StreamReader sr = new StreamReader(path)) 
{ 
    // Initialize to null so we are not stuck in loop forever in case there is nothing in the file to read 
    String line = null; 

    do 
    { 
     line = sr.ReadLine(); 

     // Is this the end of the file? 
     if (line == null) 
     { 
      // Yes, so bail out of loop 
      return; 
     } 

     // Is the line empty? 
     if (line == String.Empty) 
     { 
      // Yes, so skip it 
      continue; 
     } 

     // Here you process the non-empty line 

    } while (true); 
} 
+0

那就是我的代碼。 (除了Write不適用於我,因爲當應用程序運行第2,3,100次時,所有數據都將在一行中)。我需要每行1個日期 –

+0

你在循環中寫這些嗎? –

+0

@CocoaDev:然後預先換行:'sw.WriteLine(); sw.Write(now.ToShortDateString());'。這意味着空文本文件中的_first_項將爲空,但您可以輕鬆地手動更正它,或者在寫入之前進行檢查以確定它是否是第一行。 –

2

我可能是方式務實,簡單,但跳過所有流的東西,用File類直接像這樣...

string newLine = ""; 
if (!isFirstLine) 
    newLine = Environment.NewLine; 

File.AppendAllText(
    filePath, 
    string.Format("{0}{1}", newLine, DateTime.Now.ToString())); 
1

添加記錄應調用File.AppendAllText一件簡單的事情,正如另一個答案中指出的那樣。雖然我會建議:

File.AppendAllText(filePath, DateTime.Now.ToString() + Environment.NewLine); 

要讀取文件的最後日期也很容易:

string lastGoodLine = "01/01/1900"; 
using (StringReader sr = new StringReader(filePath)) 
{ 
    while (!sr.EndOfStream) 
    { 
     string line = sr.ReadLine(); 
     if (!string.IsNullOrEmpty(line)) 
      lastGoodLine = line; 
    } 
} 
return lastGoodLine; 
+0

乾淨而緊湊的代碼。 – Richard

相關問題