Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:39:09+00:00 2026-06-02T22:39:09+00:00

I need to get my code to read if file doesnt exist create else

  • 0

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();
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:39:13+00:00Added an answer on June 2, 2026 at 10:39 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that I need to get the source code to update
I need to get items from a html list from the vb.net code behind
I need to get the maximum value out of this code, but it's really
I need to get some random colors to draw a pie. My code works
I am writing some code in which i need to get a line from
I have a big problem with very simple code. I need to get a
How do you get the current location of iPhone through code? I need to
Currently I use this code to read a txt file with words and perform
I'm working with a C++ code base. Right now I'm using a C++ code
Basically I need to open and read a list of files I get from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.