问题
Renaming a file in c#:
File.Move(source,Destination);
File.Delete(source);
It execute successfully, how ever when i try to rename the file again, the system gives this exception: The process cannot access the file because it is being used by another process. i cant find that where is this in use? when i further debug the error it shows me the class name is in the process of w3wp.exe which is IIS. What should i do next? getting
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
this is how i am getting folder list and it is then returned to xml. then in folder i get the files when i click it
now to get the images: this is the code
public xml (string path, List<l> one)
{
List<T> tt = new List<T>();
List<T> SessionList = new List<T>();
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());
}
回答1:
You don't need to call the File.Delete as part of the rename, you would have needed it if you did a copy.
回答2:
Try using the FileShare enumerator. Then try to open a file with monesharing, close the handle and if there is no exception you can move the file.
http://msdn.microsoft.com/de-de/library/system.io.fileshare.aspx
It contains the methods Read, Write etc.
回答3:
Create a FileInfo
instance, in which you can rename it more than once.
FileInfo file = new FileInfo(source);
file.MoveTo(destination);
// execute more code
file.MoveTo(destination2)
来源:https://stackoverflow.com/questions/5152841/file-renaming-problem