首先,我认为我面临一项非常简单的任务。 但是,现在我认识到,正如我所想象的那样,它不无所作为,因此,我现在希望你们的人民能够帮助我,因为我当时 pre忙。
我的设想是(视窗2008R2服务器):
- A file gets uploaded 3 times per day to a FTP directory. The filename is always the same, which means the existing file gets overwritten every time.
- I have programed a simple C# service which is watching the FTP upload directory, I m using the FileSystemWatcher class for this.
- The upload of the file takes a few minutes, so once the File Watcher registers a change, I m periodically trying to open the file, to see if the file is still being uploaded (or locked)
- Once the file isn t locked anymore, I try to move the file over to my IIS Virtual Directory. I have to delete the old file first, and then move the new file over. This is where my problem starts. The file seems to be always locked by IIS (the w3wp.exe process).
After some research, I found out that I have to kill the process which is locking the file (w3wp.exe in this case). In order to do this, I have created a new application pool and converted the virtual directory into an application. Now my directory is running under a seperate w3wp.exe process, which I supposedly can safely kill and move the new file over there.
Now I just need to find the proper w3wp.exe process (there are 3 w3wp.exe processes running in total, each running under a seperate application pool) which has the lock on my target file. But this seems to be an almost impossible task in C#. I found many questions here on SO regarding "Finding process which locked a specific file", but none of the answers helped me. Process Explorer for example is exactly telling me which process is locking my file.
我不理解的下一个问题是,我可以通过Windows探测器删除目标文件,而没有任何问题。 就在我的C#申请中,“零正被另一个过程使用”错误。 我想知道这里有什么区别。
这里最值得注意的是关于安全局关于锁定档案和C#的问题:
^^ The example code here does actually work, but this outputs the open handle IDs for every active process. I just can t figure out how to search for a specific filename, or at least resolve the handle ID to a filename. This WinAPI stuff is way above my head.
Using C#, how does one figure out what process locked a file?
^^ The example code here is exactly what I need, but unfortunately I can t get it to work. It is always throwing an "AccessViolationException" which I can t figure out, since the sample code is making extensive use of WinAPI calls.
Simple task, impossible to do? I appreciate any help.
EDIT Here are some relevant parts of my server code:
• 帮助者在档案被锁定时发现:
private bool FileReadable(string file, int timeOutSeconds)
{
DateTime timeOut = DateTime.Now.AddSeconds(timeOutSeconds);
while (DateTime.Now < timeOut)
{
try
{
if (File.Exists(file))
{
using (FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.None))
{
return true;
}
}
return false;
}
catch (Exception)
{
Thread.Sleep(500);
}
}
m_log.LogLogic(0, "FileReadable", "Timeout after [{0}] seconds trying to open the file {1}", timeOutSeconds, file);
return false;
}
这也是我档案资料调查活动中的守则,该活动正在监测FTP上载目录。 档案馆是新上载的档案,目标档案馆是我的国际数据系统名录中的目标文件。
// here I m waiting for the newly uploaded file to be ready
if (FileReadable(filepath, FWConfig.TimeOut))
{
// move uploaded file to IIS virtual directory
string targetfilepath = Path.Combine(FWConfig.TargetPath, FWConfig.TargetFileName);
if(File.Exists(targetfilepath))
{
m_log.LogLogic(4, "ProcessFile", "Trying to delete old file first: [{0}]", targetfilepath);
// targetfilepath is the full path to my file in my IIS directory
// always fails because file is always locked my w3wp.exe :-(
if(FileReadable(targetfilepath, FWConfig.TimeOut))
File.Delete(targetfilepath);
}
File.Move(filepath, targetfilepath);
}
EDIT2: Killing the w3wp.exe process while clients are downloading the file would be no problem for us. I m just having a hard time finding the right w3wp.exe process which is locking the file.
Also, my client application, which is downloading the file on the clients, is checking the HTTP HEAD for the Last-Modified date. The client is checking the date every 10 minutes. So it is possible that the file is being locked by IIS because there are clients continously checking the HTTP HEAD for the file. Nonetheless, I don t understand why I can manually delete/rename/move the file through windows explorer without any problems. Why does this work, and why does my application get a "Locked by another process" exception?