我如何使用File.Delete(tempWbPath)从文件夹中删除文件; [英] how can i delete the files from folder using File.Delete(tempWbPath);

查看:124
本文介绍了我如何使用File.Delete(tempWbPath)从文件夹中删除文件;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在将文件复制到temp文件夹以进行快速访问.为此,我正在使用此代码.

 如果(File.Exists(tempAudpath))
{
    File.Delete(tempAudpath);
    File.Copy(Audpath,tempAudpath, true );
}
其他
{
    File.Copy(Audpath,tempAudpath, true );
}


但是删除没有发生.这表明正在使用另一个进程.
在这种情况下,谁能帮助我删除和复制文件.

[edit]忽略HTML ..."选项已禁用-OriginalGriff [/edit]

解决方案

由于异常显示文件正在被另一个进程使用",因此它不是您要删除目录-File.Delete无法做到.看到您的文件名时,这是我最初的想法.

在没有看到其余代码的情况下-并且我不希望您将其全部发布-我不能太具体,但是...

由于名称暗示这是一个临时文件,因此我只能假设您的应用程序刚刚编写了它?在这种情况下,它并不是您想像的那么简单的修复程序-问题不在此代码片段中.
发生的事情是,您编写文件的代码不够干净-您尚未处置所有访问该文件的对象.由于尚未处置一个或多个对象,因此它们仍具有附加到文件的所谓的文件句柄.由于存在这些文件,因此系统会(非常正确地)认为该文件正在使用中,并且不会让您删除它.

查看使用您的临时文件的其他代码,然后进行Dispose(或最好用using围住所有访问该文件的对象(流,binaryWriters,textWriters等).


Hi All,

I am copying my files to temp folder for quick access. For that i am using this code.

if (File.Exists(tempAudpath))
{
    File.Delete(tempAudpath);
    File.Copy(Audpath, tempAudpath, true);
}
else
{
    File.Copy(Audpath, tempAudpath, true);
}


But delete is not happening. It is showing that another process is using.
Can any one help me to delete and copy files in this scenario.

[edit]"Ignore HTML..." option disabled - OriginalGriff[/edit]

解决方案

Since the exception is showing "File in use by another process", it is not that you are trying to delete a directory - which File.Delete can''t do. That was my initial thought when I saw your file names.

Without seeing the rest of your code - and I don''t want you to post it all - I can''t be too specific, but...

Since the name implies that this is a temporary file, I can only assume that your application has just written it? In which case, it is not quite as simple a fix as you think - the problem is not in this code fragment.
What has happened is that your code to write the file is not clean enough - you have not disposes all the objects which access the file. Since one or more objects have not been disposed, they still have what are called File Handles attached to the file. Since these exist, the system assumes (quite rightly) that the file is in use, and will not let you delete it.

Look at the other code which uses your temporary file, and Dispose (or preferably surround with a using block all the objects (streams, binaryWriters, textWriters, etc.) which access it.


这篇关于我如何使用File.Delete(tempWbPath)从文件夹中删除文件;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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