System.IO.File.Delete()/ System.IO.File.Move()有时不工作 [英] System.IO.File.Delete() / System.IO.File.Move() sometimes does not work

查看:2006
本文介绍了System.IO.File.Delete()/ System.IO.File.Move()有时不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个WinForms程序需要节省一些运行时信息的XML文件。该文件有时可以是几百千字节的大小。在beta测试,我们发现有些用户会毫不犹豫地看似随意,偶尔终止进程造成在半写入的文件,因此损坏。

A Winforms program needs to save some run time information to an XML file. The file can sometimes be a couple of hundred kilobytes in size. During beta testing we found some users would not hesitate to terminate processes seemingly at random and occasionally causing the file to be half written and therefore corrupted.

因此,我们改变了算法保存到临时文件,然后删除真正的文件,做一个动作。

As such, we changed the algorithm to save to a temp file and then to delete the real file and do a move.

我们的代码,目前看起来是这样的..

Our code currently looks like this..

private void Save()
{
    XmlTextWriter streamWriter = null;
    try
    {
        streamWriter = new XmlTextWriter(xmlTempFilePath, System.Text.Encoding.UTF8);

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyCollection));

        xmlSerializer.Serialize(streamWriter, myCollection);

        if (streamWriter != null)
            streamWriter.Close();

        // Delete the original file
        System.IO.File.Delete(xmlFilePath);

        // Do a move over the top of the original file 
        System.IO.File.Move(xmlTempFilePath, xmlFilePath);
    }
    catch (System.Exception ex)
    {
        throw new InvalidOperationException("Could not save the xml file.", ex);
    }
    finally
    {
        if (streamWriter != null)
            streamWriter.Close();
    }
}

这工作在实验室和生产几乎所有的时间。该计划是在12台电脑运行,且该代码被称为平均每5分钟。约一次或一天两次,我们得到这个异常:

This works in the lab and in production almost all of the time. The program is running on 12 computers and this code is called on average once every 5 min. About once or twice a day we get this exception:

System.InvalidOperationException: 
Could not save the xml file. 
---> System.IO.IOException: Cannot create a file when that file already exists.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.File.Move(String sourceFileName, String destFileName)
at MyApp.MyNamespace.InternalSave()

这是,如果删除是在发出之前,移动实际上没有发到硬盘驱动器。

It is as if the Delete is not actually issued to the hard drive before the Move is issued.

这是发生在Win7的机器

This is happening on Win7 machines.

几个问题:是否有<$的一些概念C $ C>同花顺()可以对整个磁盘操作系统吗?这是我的代码,.NET操作系统或别的什么错误?我应该在某些 Thread.sleep代码(X)被放置?也许我应该做一个 File.Copy(SRC,DEST,真实)?我应该写下面的代码? (但它看起来非常愚蠢的。)

A couple of questions: Is there some concept of a Flush() one can do for the entire disk operating system? Is this a bug with my code, .net, the OS or something else? Should I be putting in some Thread.Sleep(x)? Maybe I should do a File.Copy(src, dest, true)? Should I write the following code? (But it looks pretty silly.)

while (System.IO.File.Exists(xmlFilePath))
{
    System.IO.File.Delete(xmlFilePath);
}

// Do a move over the top of the main file 
bool done = false;
while (!done)
{
    try
    {
        System.IO.File.Move(xmlTempFilePath, xmlFilePath);
        done = true;
    }
    catch (System.IO.IOException)
    {
        // let it loop
    }
}

已经看到了这之前?

推荐答案

怎么样,这样它会覆盖您的应用程序的状态,然后就可以删除您的临时状态使用Move.Copy与覆盖设置为true?

how about using Move.Copy with overwrite set to true so that it overwrites your app state and then you can delete your temp state ?

您还可以连接到<一个HREF =http://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit.aspx相对=nofollow> App_Exit 事件,并尝试下执行干净关闭?

You can also attach to App_Exit event and try to perform clean shut down?

这篇关于System.IO.File.Delete()/ System.IO.File.Move()有时不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆