简体   繁体   中英

File.Move then Delete not supported by Compact Framework 2.0?

The following code crashes on the second Delete call.

        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."

So it's just like File.Move(curFileName, delFileName) causes a lock (or something) on the file and doesn't release it afterward.

Note: I'm working with a Smart Device emulated by Visual Studio 2008.

I fixed it.

The problem cause was actually in a different method that was called at some point in time before the method the exception occurred.

I had a "load" method where I forgot the using clause.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM