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 对象

我使用您的 (Karl) 代码创建了一个名为“readLastDate()”的方法

我得到了第一次约会。

4

5 回答 5

2

我可能是务实和简单的方式,但跳过所有流的东西并File像这样直接使用类......

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

File.AppendAllText(
    filePath, 
    string.Format("{0}{1}", newLine, DateTime.Now.ToString()));
于 2013-07-22T20:34:39.603 回答
1

You could use a sw.Write and PRE-pend a linefeed. Unfortunately that will give you an empty line at the start of the file.

于 2013-07-22T18:54:26.953 回答
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;
于 2013-07-22T20:44:06.100 回答
1

您是否尝试过使用命令 .Trimend ('\n')?

http://msdn.microsoft.com/en-us/library/system.string.trimend.aspx

于 2013-07-22T18:43:40.257 回答
1

做这个:

sw.Write(now.ToShortDateString());

这是StreamWriter.WriteLine的 MSDN 文档。

这是StreamWriter.Write的 MSDN 文档。

更新:

继续使用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);
}
于 2013-07-22T18:21:29.013 回答