我希望从一个文件夹中的文件移动到另一个文件夹。 [英] i want file from one folder move in to another folder .

查看:115
本文介绍了我希望从一个文件夹中的文件移动到另一个文件夹。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望一个文件夹中的文件移动到另一个文件夹。



i want file from one folder move in to another folder .

string newdir = Server.MapPath("temp/hotelimg/");
          foreach (var file in Directory.GetFiles(Server.MapPath("images/hotelpic/")))
          {

              string FileName = Path.GetFileName(file);
              File.Move(file, Path.Combine(newdir, FileName));

          }





这是我的代码,但是给我错误''进程无法访问该文件,因为它正在被另一个进程使用。''

这个错误是什么?



this is my code but give me error ''The process cannot access the file because it is being used by another process.''
what is this error ?

推荐答案

这意味着什么:文件正在使用中。它可能是您当前的应用程序,也可能是另一个应用程序:但最有可能的是,当您创建文件时,您没有关闭并处理您正确使用的所有内容,因此文件将一直使用,直到应用程序结束,或垃圾收集器被执行以清理你身后。检查用于创建文件的代码。
It means what is says: the file is in use. It could be your current application, or it could be a different one: but the most likely is that when you created the file you didn't close and dispose of everything you used correctly, so the file remains in use until the app ends, or the garbage collector gets executed to clean up behind you. Check the code you used to create the file first.


引用:

该进程无法访问该文件因为它正由另一个进程使用

The process cannot access the file because it is being used by another process





这意味着您尝试移动的其中一个文件在另一个程序中打开。检查一下。



另外,我会添加一些错误处理,以查看哪个文件是移动时出错的文件。这样的事情:





This means that one of the files you are trying to move is opened in another program. Check that.

Plus, I would add some error handling to see which file was the one which gave error while moving. Something like this:

string newdir = Server.MapPath("temp/hotelimg/");
foreach (var file in Directory.GetFiles(Server.MapPath("images/hotelpic/")))
{
    try
    {
        string FileName = Path.GetFileName(file);
        File.Move(file, Path.Combine(newdir, FileName));
    }
    catch(Exception ex)
    {
        Console.WriteLine("string.Format("Error while moving File : {0}. Error Message: {1}", file, ex.Message));
    }
}


这篇关于我希望从一个文件夹中的文件移动到另一个文件夹。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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