I need to get my code to read if file doesnt exist create else append. Right now it is reading if it does exist create and append. Here is the code:
if (File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) {
Would I do this?
if (! File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) {
Edit:
string path = txtFilePath.Text; if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { foreach (var line in employeeList.Items) { sw.WriteLine(((Employee)line).FirstName); sw.WriteLine(((Employee)line).LastName); sw.WriteLine(((Employee)line).JobTitle); } } } else { StreamWriter sw = File.AppendText(path); foreach (var line in employeeList.Items) { sw.WriteLine(((Employee)line).FirstName); sw.WriteLine(((Employee)line).LastName); sw.WriteLine(((Employee)line).JobTitle); } sw.Close(); }
}
"w" - Write - will create a file if the specified file does not exist.
Create file if it does not exist by Using append modeIf the file does not exist, it creates a new file for reading and writing. It will not truncate the file and append the content at end of the file as shown in the example below.
Java creating file with FileThe File's createNewFile method creates a new, empty file named by the pathname if a file with this name does not yet exist. The createNewFile returns true if the named file does not exist and was successfully created; false if the named file already exists.
C# StreamWriter append text This constructor initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, the constructor creates a new file.
You can simply call
using (StreamWriter w = File.AppendText("log.txt"))
It will create the file if it doesn't exist and open the file for appending.
Edit:
This is sufficient:
string path = txtFilePath.Text; using(StreamWriter sw = File.AppendText(path)) { foreach (var line in employeeList.Items) { Employee e = (Employee)line; // unbox once sw.WriteLine(e.FirstName); sw.WriteLine(e.LastName); sw.WriteLine(e.JobTitle); } }
But if you insist on checking first, you can do something like this, but I don't see the point.
string path = txtFilePath.Text; using (StreamWriter sw = (File.Exists(path)) ? File.AppendText(path) : File.CreateText(path)) { foreach (var line in employeeList.Items) { sw.WriteLine(((Employee)line).FirstName); sw.WriteLine(((Employee)line).LastName); sw.WriteLine(((Employee)line).JobTitle); } }
Also, one thing to point out with your code is that you're doing a lot of unnecessary unboxing. If you have to use a plain (non-generic) collection like ArrayList
, then unbox the object once and use the reference.
However, I perfer to use List<>
for my collections:
public class EmployeeList : List<Employee>
or:
using FileStream fileStream = File.Open(path, FileMode.Append); using StreamWriter file = new StreamWriter(fileStream); // ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With