windows 重命名正在运行的可执行 (exe) 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6695496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename a running executable (exe) file
提问by Astin
We are trying to push updates to multiple servers at once and my manager has found that it is possible to rename running .exe file. Using that knowledge he wants to rename a running exe and copy over a new version of said exe such that anyone running their in memory copy of foo.exe are fine and anybody who opens a shortcut pointing to foo.exe will get a new copy with updates applied.
我们正在尝试一次将更新推送到多台服务器,我的经理发现可以重命名正在运行的 .exe 文件。使用这些知识,他想重命名一个正在运行的 exe 并复制该 exe 的新版本,这样任何人在内存中运行 foo.exe 副本都很好,任何打开指向 foo.exe 的快捷方式的人都将获得一个新副本已应用更新。
I guess I need to clarify, He doesn't expect the old copy to magically update, he just expects them to keep running the old copy until they open the exe again, in which case it will then open the new one that has the name of the old one.
我想我需要澄清一下,他不希望旧副本神奇地更新,他只是希望他们继续运行旧副本,直到他们再次打开 exe,在这种情况下,它将打开具有名称的新副本旧的。
It sometimes throws an exception that the file is in use on his program but if he tries renaming it in a loop it will eventually succeed. On my machine I have yet to be able to get it to work even in a loop.
它有时会抛出一个异常,该文件正在他的程序中使用,但如果他尝试在循环中重命名它,它最终会成功。在我的机器上,即使在循环中,我也无法让它工作。
My first and main question is this: Is it ever acceptable to do this. Should renaming a running executable ever be a valid scenario?
我的第一个也是主要的问题是:这样做是否可以接受。重命名正在运行的可执行文件应该是一个有效的场景吗?
Secondly, if it is a valid scenario then how could one reliably do this? Our current thoughts are try a bunch of times using File.Move (C#) to do a rename and if it doesn't work then write out to an error log so it can be handled manually.
其次,如果这是一个有效的场景,那么如何可靠地做到这一点?我们目前的想法是尝试多次使用 File.Move (C#) 进行重命名,如果不起作用,则将其写入错误日志,以便手动处理。
回答by Eric Lippert
An airplane mechanic and a surgeon meet in a bar. The mechanic says "you know, we have basically the same job. We take broken stuff out and put new, better parts in." The surgeon says "yeah, but you don't have to keep the plane flying as you're making the repairs!"
飞机修理工和外科医生在酒吧见面。机械师说:“你知道,我们的工作基本上是一样的。我们把坏掉的东西拿出来,把新的、更好的零件放进去。” 外科医生说:“是的,但你不必在修理时让飞机保持飞行!”
Trying to update an application by moving files while the application is running seems about as dangerous as trying to fix an airplane in flight. Possible? Sure. Greatly increased risk of catestrophic crash? Yep.
在应用程序运行时尝试通过移动文件来更新应用程序似乎与尝试修理飞行中的飞机一样危险。可能的?当然。大大增加了灾难性撞车的风险?是的。
If the application you are updating is a managed application, consider using ClickOnce Deployment. That way, the next time someone runs the application, if there is a new version available it will be copied down and installed automatically. That's much more safe and pleasant than trying to mess with an application while its still running.
如果您要更新的应用程序是托管应用程序,请考虑使用ClickOnce 部署。这样,下次有人运行该应用程序时,如果有可用的新版本,它将被复制下来并自动安装。这比在应用程序仍在运行时试图弄乱它要安全和愉快得多。
回答by Cody Gray
No, this is not acceptable. Do not do this. This is not a valid deployment mechanism. This should have been yours or his first clue:
It sometimes throws an exception that the file is in useon his program but if he tries renaming it in a loop it will eventually succeed.
And it won't work, anyway. His theory is quite wrong:
Using that knowledge he wants to rename a running exe and copy over a new version of said exe such that anyone running their in memory copy of foo.exe are fine and anybody who opens a shortcut pointing to foo.exe will get a new copy with updates applied.
Specifically, the copy in memory will not be automatically replaced with the new executable just because it has the same name. The reason that you're allowed to rename the executable in the first place is because the operating system is not using the file name to find the application. The original executable will still be loaded, and it will remain loaded until you explicitly unload it and load the new, modified executable.
Notice how even modern web browsers like Chrome and Firefox with their super fancy automatic, in the background, no one ever notices that they exist, updaters still have to close and relaunch the applicationin order to apply the updates.
Don't worry about shooting the messenger here. It's more likely that your customers and your tech support department will shoot you first.
See number 1.
不,这是不可接受的。不要这样做。这不是有效的部署机制。这应该是你或他的第一个线索:
它有时会抛出一个异常,该文件正在他的程序中使用,但如果他尝试在循环中重命名它,它最终会成功。
而且无论如何都行不通。他的理论是完全错误的:
使用这些知识,他想重命名一个正在运行的 exe 并复制该 exe 的新版本,这样任何人在内存中运行 foo.exe 副本都很好,任何打开指向 foo.exe 的快捷方式的人都将获得一个新副本已应用更新。
具体来说,内存中的副本不会因为新的可执行文件具有相同的名称而被自动替换。首先允许您重命名可执行文件的原因是操作系统没有使用文件名来查找应用程序。原始可执行文件仍将被加载,并且它将保持加载状态,直到您明确卸载它并加载新的、修改过的可执行文件。
请注意,即使是像 Chrome 和 Firefox 这样的现代网络浏览器,在后台,也没有人注意到它们的存在,更新程序仍然需要关闭并重新启动应用程序才能应用更新。
不用担心在这里射杀信使。您的客户和您的技术支持部门更有可能首先向您开枪。
见编号 1。
回答by Viv
In our organization, we solved the problem of Updates by having two release folders say EXE_A and EXE_B. We also have a release folder called EXE which only has links ALL of which points to either to EXE_A or EXE_B from which the user runs the applications.
在我们的组织中,我们通过使用两个发布文件夹 EXE_A 和 EXE_B 来解决更新问题。我们还有一个名为 EXE 的发布文件夹,它只有指向用户运行应用程序的 EXE_A 或 EXE_B 的链接。
When we publish a new version of the program, we publish it to the folder that is not referenced in the links and then update the links (EXE). In this way, you do not get into exceptions that users are holding the application / assemblies. Also if a user wants to run the updated version, all he need to do is close / re-execute the link in EXE folder.
当我们发布程序的新版本时,我们将其发布到链接中未引用的文件夹中,然后更新链接(EXE)。这样,您就不会遇到用户持有应用程序/程序集的例外情况。此外,如果用户想要运行更新版本,他需要做的就是关闭/重新执行 EXE 文件夹中的链接。
回答by Christoph Fink
If you use Windows Vista/Server2k8 or newer you could use mklink
to create a symbolic link to the folder containing your application and start the application out of the "symblic linked folder" and then at the update create a new folder, e.g. "AppV2" and change the SymLink to that folder, so the next time the user restarts the application he starts it out of the new folder without noticing.
如果您使用 Windows Vista/Server2k8 或更新版本,您可以使用mklink
创建一个符号链接到包含您的应用程序的文件夹并从“符号链接文件夹”启动应用程序,然后在更新时创建一个新文件夹,例如“AppV2”和将 SymLink 更改为该文件夹,因此下次用户重新启动应用程序时,他会在没有注意到的情况下从新文件夹中启动它。
Renaming open files is ALWAYS a bad choice!
重命名打开的文件总是一个糟糕的选择!
But in general I would think of a better deployment strategy anyway, because if you need to use such "hacks" it is always a messy situation. I don't know your application, but maybee ClickOnce would be a point to start, because you can configure it to check for updates on every start...
但总的来说,无论如何我都会想到更好的部署策略,因为如果您需要使用这样的“黑客”,它总是很混乱。我不知道你的应用程序,但也许 ClickOnce 是一个开始点,因为你可以将它配置为在每次启动时检查更新......