Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent adding an empty line to my file?
    text
    copied!<p>Im creating a text file and the last line is ""</p> <pre><code> 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) &amp;&amp; (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); } } </code></pre> <p>is what I am using to create the file (or append it)</p> <p>now is a DateTime object</p> <p>I used your (Karl) code to create a method called "readLastDate()"</p> <p>I get the 1st date instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload