[英]How to prevent adding an empty line to my file?
Im creating a text file and the last line is "" 我正在创建一个文本文件,最后一行是“”
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);
}
}
is what I am using to create the file (or append it) 是我用来创建文件(或附加文件)的东西
now is a DateTime object 现在是一个DateTime对象
I used your (Karl) code to create a method called "readLastDate()" 我用您的(Karl)代码创建了一个名为“ readLastDate()”的方法
I get the 1st date instead. 我得到了第一个约会。
I'm probably being way to pragmatic and simple, but skip all the stream stuff and use File
class directly like this... 我可能正在以务实和简单的方式,但是跳过所有流内容并像这样直接使用
File
类...
string newLine = "";
if (!isFirstLine)
newLine = Environment.NewLine;
File.AppendAllText(
filePath,
string.Format("{0}{1}", newLine, DateTime.Now.ToString()));
Do this: 做这个:
sw.Write(now.ToShortDateString());
Here is the MSDN documentation for StreamWriter.WriteLine . 这是StreamWriter.WriteLine的MSDN文档。
Here is the MSDN documentation for StreamWriter.Write . 这是StreamWriter.Write的MSDN文档。
UPDATE: 更新:
Keep using the WriteLine
, but change the way you read your values in from the file: 继续使用
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);
}
Have you tried using the command .Trimend ('\\n')? 您是否尝试过使用命令.Trimend('\\ n')?
http://msdn.microsoft.com/en-us/library/system.string.trimend.aspx http://msdn.microsoft.com/zh-CN/library/system.string.trimend.aspx
You could use a sw.Write
and PRE-pend a linefeed. 您可以使用
sw.Write
并预先添加换行符。 Unfortunately that will give you an empty line at the start of the file. 不幸的是,这将在文件开头为空行。
Adding a record should be a simple matter of calling File.AppendAllText
, as pointed out in another answer. 如另一个答案所指出的,添加记录应该是调用
File.AppendAllText
的简单问题。 Although I would recommend: 尽管我建议:
File.AppendAllText(filePath, DateTime.Now.ToString() + Environment.NewLine);
To read the last date from the file is also very easy: 从文件中读取最后一个日期也很容易:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.