题
我有一个更新程序,它是完全独立的我的主应用程序的。我运行Update.exe更新APP.EXE。要检查是否该文件正在使用中,我把它移动到另一个文件,并赶上一个错误,如果它是在使用中。如果没有问题occurr我将其重命名。至少这是我的计划......
主程序:APP.EXE结果 更新程序:Update.exe的
此程序用于在网络上没有任何服务的运行。因此,用户被毫不夸张地运行thier机器上通过网络的exe。
我需要的Update.exe已经运行时更新APP.EXE。要检查,看看是否APP.EXE仍在使用我包裹在一个try以下/ catch语句,看它是否失败:
IO.File.Move(upddir& “\ APP.EXE”,upddir& “\ app.exe.tst”),点击 IO.File.Move(upddir& “\ app.exe.tst”,upddir& “\ APP.EXE”)
有趣的是,即使APP.EXE运行,此举可以将其重命名为app.exe.tst没有任何错误。我甚至可以继续在没有任何错误的应用程序。
我以为我是在我心里,让我不得不另一个程序员看看这个,他证实了我上面说的。
因此,我们尝试这种包装在一个尝试捕捉:
昏暗ReadFile的作为新的FileStream(upddir& “\ APP.EXE”,FileMode.Open,FileAccess.Read,FileShare.None),点击 readfile.Dispose()
我把文件共享作为没有,至少我认为它会,表明有一个人在文件中。
它仍然继续进行,没有任何错误。
任何人都知道为什么我可以重命名使用中的文件? 此外,有没有更好的方法来做到这一点比什么,我现在在做什么?
谢谢! Eroc
解决方案
您可以执行以下操作来检查应用程序运行的其他实例:
Process.GetProcessesByName("app.exe", "Machine1").Length > 0
哪些是,如果应用程序正在运行检查的一个更合适的方式。
看一看 File.Move 一> MSDN文档。它详细介绍了抛出异常的东西。你被允许重新命名,即使它是在Vista中使用一个exe。
其他提示
使用的 “使用” 同代码:)
Public Function FileInUse(ByVal sFile As String) As Boolean
Dim thisFileInUse As Boolean = False
If System.IO.File.Exists(sFile) Then
Try
Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
' thisFileInUse = False
End Using
Catch
thisFileInUse = True
End Try
End If
Return thisFileInUse
End Function
你可以做的是更换一个看守政府EXE目标应用程序。
此看守然后衍生为目标应用程序,但还产生在共享位置锁定的文件。锁定被释放,并且文件被删除时,应用程序退出。
然后,在更新之前APP.EXE,你检查是否有在共享位置的任何锁定的文件,如果存在锁定的文件则APP.EXE正在使用中。
在看守程序可能是一个窗口应用较少,下面的控制台应用程序确实差不多就是它需要做
class Program
{
static string lockFileName;
static System.IO.StreamWriter lockFileWrtier;
static void Main(string[] args)
{
lockFileName = "AppLock_" + System.IO.Path.GetRandomFileName();
lockFileWrtier = new System.IO.StreamWriter(lockFileName);
System.Diagnostics.Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
p.Start();
Console.WriteLine("Press any key to stop");
Console.ReadKey();
}
static void p_Exited(object sender, EventArgs e)
{
lockFileWrtier.Dispose();
System.IO.File.Delete(lockFileName);
Console.WriteLine("Process has exited");
}
}
在更新,然后查找所有“AppLock_ *”的文件,并试图用写锁打开它们,如果它不能在其中一人得到一个写锁,exe文件正在使用中。
希望这有助于。
这是我落得这样做。网络扫描没有一个对等网络上工作。它根本不解决其他计算机。
总之,在这里它是:
私人小组CheckFileIfFileIsInUse(BYVAL thefilename作为字符串),点击 尝试点击 “由于Vista操作系统,你可以重命名文件正在使用中点击 '的‘在原始文件的使用’紧跟在新的文件名,点击
Dim testfile As String = thefilename & ".tst"<br /> If IO.File.Exists(testfile) Then<br /> IO.File.Delete(testfile)<br /> End If<br /> ' so we have to rename it to something else<br /> IO.File.Move(thefilename, testfile)<br /> ' copy it back to the original file name in case something <br />breaks ' this just keeps them in working order if it does<br /> IO.File.Copy(testfile, thefilename)<br /> ' then we try to delete the original file that could be in use <br /> ' which will return an 'in use' error at this point<br /> If IO.File.Exists(testfile) Then<br /> IO.File.Delete(testfile)<br /> End If<br /> Catch ex As Exception<br /> 'throw it to the originating method<br /> Throw<br /> End Try<br />
结束子结果
希望这有助于在未来的人。
Public Function FileInUse(ByVal sFile As String) As Boolean
If System.IO.File.Exists(sFile) Then
Try
Dim F As Short = FreeFile()
FileOpen(F, sFile, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite)
FileClose(F)
Catch
Return True
End Try
End If
End Function