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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:35:01+00:00 2026-05-26T11:35:01+00:00

I have following code: private void askforlocation() { if (File.Exists(location.txt)) { System.IO.StreamReader loc =

  • 0

I have following code:

private void askforlocation()
        {
            if (File.Exists("location.txt"))
            {
                System.IO.StreamReader loc = new System.IO.StreamReader("location.txt");
                string loca = loc.ReadToEnd();
                if (loca != "")
                {
                    int index = comboBox1.FindString(loca);
                    comboBox1.SelectedIndex = index;
                }
                else
                {
                    label6.Text = "Please select the location!";
                }
                loc.Close();
            }
            else label6.Text = "Please select the location!";        
        }

It is supposed to read value “location” from the file and put it to the combo box, which works ok.

I run this script on Form1_Load.

Now, I have another script:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = comboBox1.SelectedItem.ToString();
    System.IO.File.WriteAllText("location.txt", value);
}

This one is supposed to record the choice so that user doesn’t need to enter location every time.

What is happening is when I start a program, so the value is already set, then I try to change it (so that theoretically it should overwrite the previous one), but I get an exception, saying that file is already being used by another process.

I do close the file after I used it. I also tried FILE.DISPOSE.

What am I doing wrong?

  • 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-05-26T11:35:02+00:00Added an answer on May 26, 2026 at 11:35 am

    I think what’s happening here is that this code:

    if (loca != "")
    {
        int index = comboBox1.FindString(loca);
        comboBox1.SelectedIndex = index;
    }
    

    is causing the SelectedIndexChanged event to be raised on the combobox. When that event is raised, comboBox1_SelectedIndexChanged is called, and that method again tries to access location.txt.

    To fix, I would first change the code in askforlocation to something like this:

    if (File.Exists("location.txt"))
    {
        var loca = string.Emtpy;
        using(var loc = new System.IO.StreamReader("location.txt"))
        {
             loca = loc.ReadToEnd();
        }
        ....
    }
    

    since there’s no need to keep the file open for longer than necessary (note that the using block will call the Dispose() method on the StreamReader when it exits, which in turn will call the Close() method). After that, I’d consider coming up with a way to keep the event from being fired when you set the selected index on the combobox (maybe use a flag or unwire/rewire the event handler).

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

Sidebar

Related Questions

I have the following code: private void SetControlNumbers() { string controlString = ; int
I have following code: private void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e) { System.Diagnostics.Process execute =
i have following code ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadAsync), apiMethod); downloadHandle.WaitOne(); Where DownloadAsync is private void DownloadAsync(object
I have the following code: SaveFileDialog saveGC1File = new SaveFileDialog(); private void GCSavePlacementOneButton_Click(object sender,
I have the following code: private void Process(string path) { using (FileStream fs =
I have the following code - private static void convert() { string csv =
hi i have the following code: private void Textparsing() { using (StreamReader sr =
I have following code: private void ProcessQueue() { foreach (MessageQueueItem item in GetNextQueuedItem()) PerformAction(item);
I have the following code: private static void WriteStartupInfo() { Settings settings = Settings.Default;
I have the following code: [TestMethod] public void TestFoo() { Foo(null); } private void

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.