troubleshooting Question

file is getting used by another process

Avatar of Bytech India
Bytech India asked on
C#
9 Comments5 Solutions402 ViewsLast Modified:
getting error sometimes: System.IO.IOException, process can not access file UserKey.txt as it is getting used by another process.

UserInfoFile = Application.StartupPath.Replace(@"\bin\Debug", "");
UserInfoFile = UserInfoFile.Replace(@"\bin\Release", "");
UserInfoFile = UserInfoFile + "\\Resources\\UserKey.txt";

  if (checkBox3.Checked)
            {
                try
                {
                    fs = new FileStream(UserInfoFile, FileMode.Open, FileAccess.ReadWrite);
                    SW = new StreamWriter(fs);
                    // SW = new StreamWriter(UserInfoFile);
                    SW.AutoFlush = true;
                    SW.WriteLine(Result1);
                    SW.Flush();
                    SW.Close();
                    SW.Dispose();
                    fs.Close();
                    fs.Dispose();
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
            else
            {
                try
                {
                    System.IO.File.Delete(UserInfoFile);
                    System.IO.File.Create(UserInfoFile);
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 5 Answers and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 5 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros