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 8739137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:52:20+00:00 2026-06-13T10:52:20+00:00

When exploring options for creating text files I stumbled across this behavior I cant

  • 0

When exploring options for creating text files I stumbled across this behavior I cant explain

Both buttons will create a file

but when I create a file with button1 button2 generates an error.

This only occurs when the file is actually created.

After a file is created buttons 1 and two operate as expected

Sample code for a simple GUI program 2 buttons and a multilne text box enclosed

    string logFile;

    public Form1()
    {
        InitializeComponent();

        logFile = "test.txt";

    }

    private void button1_Click(object sender, EventArgs e)
    {

        if (!System.IO.File.Exists(logFile))
        {
            System.IO.File.Create(logFile);
            textBox1.AppendText("File Created\r\n");
        }
        else
        {
            textBox1.AppendText("File Already Exists\r\n");
        }

        System.IO.File.AppendText("aaa");

    }

    private void button2_Click(object sender, EventArgs e)
    {

        // 7 overloads for creation of the Stream Writer

        bool appendtofile = true;

        System.IO.StreamWriter sw;

        try
        {
            sw = new System.IO.StreamWriter(logFile, appendtofile, System.Text.Encoding.ASCII);
            sw.WriteLine("test");
            textBox1.AppendText("Added To File Created if Needed\r\n");
            sw.Close();

        }
        catch (Exception ex)
        {
            textBox1.AppendText("Failed to Create or Add\r\n");

            // note this happens if button 1 is pushed creating the file 
            // before button 2 is pushed 
            // eventually this appears to resolve itself

            textBox1.AppendText("\r\n");
            textBox1.AppendText(ex.ToString());
            textBox1.AppendText("\r\n");
            textBox1.AppendText(ex.Message);
        }



    }
  • 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-13T10:52:21+00:00Added an answer on June 13, 2026 at 10:52 am

    You might be getting an error because file resource is in use by the previous process. While using resources like file (and using a StreamWriter object, which is IDisposable), it is advised to use the “Using” directive. This will close the resource once code execution completes.

     using (StreamWriter sw = File.CreateText(path)) 
                {
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }   
    

    Once you write the third line, file will automatically be closed and resources disposed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Messing about with interface builder for the first time, exploring my options. Text seems
I am attempting to load multiple images in AS3 and I'm exploring different options
I've been exploring methods for full-width background images combined with type. I found this
I'm currently exploring the different options for building a not-too-complex web application, in which
I'm creating a query that will display information for a record which is derived
I'm exploring my options for modifying urls in the browser bar for bookmarking purposes.
This is a fairly generic question in that I'm just wondering what potential options
I'm new to Core Data. I'm creating an app that will allow a user
I'm creating an air application that allows the user to create, edit and save
I'm exploring the Android API options about the battery. what possibilities grants the permission

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.