1

I just create a class named WriteLog,this class contains the following methods:

    <!-- language: c# -->
    //First Version:
    public void Add(string location, string words)
    {
        StreamWriter sw = new StreamWriter(location);
        sw.WriteLine(words);
        sw.Close()
    }
    //Using Version:
    public void Add(string location, string words)
    {
        using (StreamWriter sw = File.AppendText(location))
        {
            sw.WriteLine(words);
        }
    }
    //My file path:
    string test = Path.GetDirectoryName(path) + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".json";
    //Where the Exception throwed:
    WriteLog.Add(ltn, time);  
    //Code..  
    File.Delete(ltn);  

But,there was a problem when I called it this way:
it throwed IOException:
This file is in use by another process and cannot be accessed by that process.
Even using cannot solve the problem.
I have verified that:
1.I comment out this code(WriteLog.Add),and Exception wasn't throwed again.
2.My file path is OK,and my application can access the path.
3.No Inner Exceptions.
I also tried Close, Dispose and Flush,and even tried to use them at a time,but it doesn't work.
Who have a solution?

PS:I'm a Chinese,so my grammar probably isn't OK.Sincere apology.

Yyh
  • 103
  • 7
  • sorry,it's a wrong tag.it's c#. – Yyh Jan 11 '20 at 04:23
  • 2
    Please review [MCVE] guidelines on posting code and confirm that sample you have actually enough to demonstrate the problem. As it stands now it's not possible to know what path you use as `ltn` is never even defined in code shown. – Alexei Levenkov Jan 11 '20 at 04:25
  • Saying *"I also tried Close, Dispose, and Flush, ..."* isn't enough. You need to show *how* you tried them (in particular, where you did it relation to other parts of the code); just because you used them doesn't mean you used them correctly. If you *show* what you tried instead of *telling*, you'll have a better chance of getting a good answer. – madreflection Jan 11 '20 at 04:40
  • It happened just because I don't dispose another writer.I'm truly sorry to all of you. – Yyh Jan 11 '20 at 05:59

0 Answers0