We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

mylog file is being used by another process?

IzzyTwinkly
IzzyTwinkly asked
on
Medium Priority
511 Views
Last Modified: 2012-08-14
Hi,
When I click the button, it calls MyWorld method.
As u can see, MyWorld() checks if 'mylog.txt' is in the path.  If it isn't, it should be created.
Then, it calls YourMethod in Class1.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MyWorld();
        }
        private void MyWorld()//<--this is in Form1.
        {  
            string mylogpath = "D:\\mylog.txt";
            if (File.Exists(mylogpath) == false)
            {
                File.Create(mylogpath);
            }                    
            Class1.YourMethod( mylogpath, this); //<--YourMethod is in Class1 class.    
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WindowsFormsApplication1
{
    static class Class1
    {
        public static void YourMethod(string logpath, Form1 f1)
        {
            string str = f1.textBox1.Text + "\n";
            StreamWriter sw = File.AppendText(logpath);
            string mystr = str + "\n";
            sw.WriteLine(mystr);
            sw.WriteLine();
            sw.Close();
        }

    }
}

When I click the button, the error displays "The process cannot access the file 'D:\\mylog.txt' because it is being used by another process.
This is happening only when there is no mylog.txt in the path.  Where there is mylog.txt in the path, it works fine.

What's the problem?  what should I fix here?
     
Comment
Watch Question

Jens FiedererTest Developer/Validator

Commented:
Your "Create" is opening a new FileStream.  Close that.
Test Developer/Validator
Commented:
FileStream stream = File.Create(mylogpath);
if (stream != null)
{
         stream.Close();
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
What a valuable lesson u taught me! =)
Jens FiedererTest Developer/Validator

Commented:
How gracious!  
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.