2013-07-22 29 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

相关问题