English 中文(简体)
未能按部就班地超编地将档案列入国际空间法学会虚拟名录/应用(档案总是锁定)
原标题:Failing to programatically overwrite a file in an IIS Virtual Directory/Application (file is always locked)

首先,我认为我面临一项非常简单的任务。 但是,现在我认识到,正如我所想象的那样,它不无所作为,因此,我现在希望你们的人民能够帮助我,因为我当时 pre忙。

我的设想是(视窗2008R2服务器):

  1. 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.
  2. I have programed a simple C# service which is watching the FTP upload directory, I m using the FileSystemWatcher class for this.
  3. 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)
  4. 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#的问题:

Win32: 如何获得拥有微粒的工艺/阅读?

^^ 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?

最佳回答

我提出的一个问题是,在文件仍在撰写期间,档案已经存在,这意味着它也会被锁定。 如果您的档案(Readable())职能目前被召回,那将是假的。

我的解决办法是,在撰写档案的正文中,将档案写到(例如)OUTPUT1。 越野,越野,越发书写,文件Stream就关闭,就更名为OUTPUT2。 LEXT。 这样,即存在成果方案。 TMT表示,档案是书面的,(全心全意)没有锁。 简单地检查了EPAPUT2。 载于您的FileReadable () loop。

问题回答

每个人都说......

"Do it a better way"

Nobody say how!!!

这里是这样。 由于你提到了我的客户申请,在此,如果你不控制阅读档案,就有一个关键的机会。

Just use new filenames each time.

你们掌握了阅读和撰写文件的程序。 在档案中增加数字,客户最多(实际上是最新日期,然后是你的数字可以总结)。 作者方案是否清理了旧的档案,如果它能够;如果没有,他们会赢得任何伤害。 国际空间法学会最终会让它们走下去。 如果不是的话,每星期就开放探索者,这样做!

导致这项工作的其他关键因素是更新的频率低(一些档案胜过坏),以及FTP+网络服务器也处于同一运动之中(否则,电传系统不是原子的,客户可以获得半拷贝的档案)。 如果FTP驱动力不同,解决办法是复制到网络服务器的驾驶时。

,如果你能够改变客户,或者只读一个名字,那么:

前言有文字。 客户打过了X射线X,把吉大港山区右头放在头上,有正确的档案逻辑,并 out出档案内容。 这是一种非常受欢迎的切页,用于在数据库中向浏览器发送存储图像,而成像器似乎从档案中读过。 (沿这一行,样本代码)。

< 声像一个 ha,但不是。 现代无锁的记忆系统也一样。 不可能出现24小时或腐败;在书写完成之前,读者可看到旧版本。

<+<>>>,它简单明了,每个人,从一个字母的基德到旁遮普,都会确切知道你们的记忆。 低技术!

我看不出你在座标本上。 保持档案库的开放将保持一个锁。 关闭上游是一个好的想法。 你也许不想像其他人在这里提到的那样,杀害你的3wp.exe进程。

重新启用国际船舶和公司能够打开由W3wp.exe接手的档案。

cmd (run as administrator) -> iisreset /stop -> update/delete file in windows explorer -> iisreset /start





相关问题
Session Management with Windows Authentication

In an ASP.NET web app, using Integrated Windows Authentication, is the session tied to the windows identity? In other words, if I login (using IWA) to the app, and the app stores some "stuff" in my ...

Using Elmah with Cassini

Does anyone know if I can use Elmah with Visual Studio build-in web server(aka Cassini)? I get it working easily on IIS, but using same configuration, it doesn t work with Cassini. When I requested ...

Setting hostname in IIS, include www?

I want to set the hostname for a website I m adding in IIS 7, however do I include the www in the hostname or not? because I want both www.mysite.com and mysite.com both to point to mysite on the ...

inetpub versus any other folder

I ve run websites out of inetpub, as well as from folders just residing on the C: drive. I wonder, are there any definitive advantages to running websites out of inetputwwwroot?

IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

热门标签