1

以下代码在第二次 Delete 调用时崩溃。

        using (var str = new StreamWriter(newFileName))
        {
            foreach (Entry entry in this.Entries)
            {
                str.WriteLine(
                    String.Format(
                        @"""{0}"";{1:yyyy-dd-MMThh:mm:ss};""none"""
                        , entry.Data
                        , entry.Date
                    )
                );
            }
        }

            File.Delete(delFileName);
            File.Move(curFileName, delFileName);
            File.Move(newFileName, curFileName);
            File.Delete(delFileName); // Crash

"The process can not access the file '\\asld.csv' because it is being used by another process."

所以这就像File.Move(curFileName, delFileName)导致文件上的锁定(或其他东西)并且之后不会释放它。

注意:我正在使用由 Visual Studio 2008 模拟的智能设备。

4

1 回答 1

0

我修好了它。

问题原因实际上是在发生异常之前的某个时间点调用的不同方法中。

我有一个“加载”方法,我忘记了 using 子句。

    private void load()
    {
        this.lstEntries = new List<Entry>();

        var delFileName = String.Format(@"{1}\{0}d.csv", this.FilePrefix, this.Folder);
        var curFileName = String.Format(@"{1}\{0}c.csv", this.FilePrefix, this.Folder);

        if (File.Exists(delFileName) && !File.Exists(curFileName))
        {
            curFileName = delFileName;
        }

        if (File.Exists(curFileName))
        {
            using (var str = new StreamReader(curFileName)) // fixed with using
于 2014-03-10T11:17:02.273 回答