文件重命名问题?

在c#中重命名文件:

File.Move(source,Destination); File.Delete(source); 

它成功执行,但是当我再次尝试重命名文件时,系统会给出以下exception: 进程无法访问该文件,因为它正被另一个进程使用。 我无法发现它在哪里使用? 当我进一步调试错误时,它显示我的类名是在w3wp.exe的进程中,这是IIS。 我接下来该怎么办? 越来越

 foreach (string folder in folder) { FileSystemItem item = new FileSystemItem(); DirectoryInfo di = new DirectoryInfo(folder); item.Name = di.Name; item.FullName = di.FullName; item.Path = path + "\\" + item.Name; item.CreatedDate = di.CreationTime; item.IsFolder = true; item.Extension = "folder"; listFolder.Add(item); } docList = CreatXmllist(listFolder); return docList 

这是我如何获取文件夹列表,然后它返回到xml。 然后在文件夹中我点击它时获取文件

现在来获取图像:这是代码

 public xml (string path, List one) { List tt = new List(); List SessionList = new List(); string[] files = Directory.GetFiles(HttpContext.Current.Request.PhysicalApplicationPath + path); foreach (string file in files) { FileSystemItem item = new FileSystemItem(); FileInfo i = new FileInfo(file); string a = i.LastWriteTime.ToString(); var thumbnails = from a in b where a.Name == fi.Name select t; if (fi.Name != "a") if (t.Count() == 0) { r session r = new r(); r.aName = fi.aName; SessionList.Add(r); fi.Exists; } else t.Add((T)t.First()); } 

您不需要将File.Delete作为重命名的一部分来调用,如果您复制了该文件,则需要它。

尝试使用FileShare枚举器。 然后尝试打开带有monesharing的文件,关闭句柄,如果没有exception,则可以移动文件。

http://msdn.microsoft.com/de-de/library/system.io.fileshare.aspx

它包含Read,Write等方法。

创建一个FileInfo实例,您可以在其中多次重命名该实例。

 FileInfo file = new FileInfo(source); file.MoveTo(destination); // execute more code file.MoveTo(destination2)