1

我只是创建了一个名为 WriteLog 的类,该类包含以下方法:

    <!-- 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);  

但是,当我这样调用它时出现了一个问题:
它抛出 IOException:
This file is in use by another process and cannot be used by the process.
即使使用也不能解决问题。
我已经验证了:
1.我注释掉了这段代码(WriteLog.Add),并且没有再次抛出异常。
2.我的文件路径没问题,我的应用程序可以访问该路径。
3.没有内在的例外。
我也尝试过关闭、处理和刷新,甚至尝试一次使用它们,但它不起作用。
谁有解决方案?

PS:我是中国人,所以我的语法可能不对。真诚道歉。

4

0 回答 0