Check if file is locked or not and present to end user if it is locked and by what.

Resulted in breaking some of my habits and putting a messagebox windows in libraries.  Not my favorite thing, but it works.

This helps debug issue 3, it does not resolve issue 3.

To test it - open an office document (such as Word or Excel) and try to overwrite the office document with the RDP file including the office document extention (xls, xlsx, doc, docx, etc).  The application will tell you that the file is locked, locked by Word/Excel/etc and the PID that is locking it just in case you have multiple copies of the application that is locking it open.

A lot of the code was borrowed from Microsoft (https://code.msdn.microsoft.com/windowsapps/How-to-know-the-process-170ed5f3/sourcecode?fileId=151114&pathId=1558127374).
This commit is contained in:
brianga
2019-11-13 13:09:43 -06:00
parent f7a343e216
commit 57111a3d21
19 changed files with 808 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
Imports System.Reflection.Assembly
Imports System.Windows.Forms
Public Class RDP
Private rdpFilePath As String
@@ -67,16 +67,45 @@ Public Class RDP
'Define temp files to delete
Dim FilesToDelete As List(Of String) = New List(Of String)(New String() {wxsPath, wixobjPath, wixpdbPath})
Dim LockCheck As New LockChecker.LockChecker()
Dim FileLocked As String
Dim SkipFile As Boolean = False
'Check if rdp file is already in TEMP folder
If rdpParentFolder = TempPath Then rdpInTemp = True
'if RDP file not in temp, copy to temp
If Not rdpInTemp Then
My.Computer.FileSystem.CopyFile(rdpFilePath, rdpTempPath, True)
FileLocked = LockCheck.CheckLock(rdpTempPath)
While Not (FileLocked = "No locks")
If (MessageBox.Show("The file " + rdpTempPath + " is currently locked. Lock information:" + FileLocked + vbNewLine + "Do you want to try again?", "File Locked", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
FileLocked = LockCheck.CheckLock(rdpTempPath)
Else
MessageBox.Show("The following file will not be deleted:" + vbNewLine + rdpTempPath)
SkipFile = True
FileLocked = "No locks"
End If
End While
If Not (SkipFile) Then
My.Computer.FileSystem.CopyFile(rdpFilePath, rdpTempPath, True)
End If
FilesToDelete.Add(rdpTempPath)
If hasIcon Then
My.Computer.FileSystem.CopyFile(IconFilePath, icoTempPath, True)
FileLocked = LockCheck.CheckLock(icoTempPath)
While Not (FileLocked = "No locks")
If (MessageBox.Show("The file " + icoTempPath + " is currently locked. Lock information:" + FileLocked + vbNewLine + "Do you want to try again?", "File Locked", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
FileLocked = LockCheck.CheckLock(icoTempPath)
Else
MessageBox.Show("The following file will not be deleted:" + vbNewLine + icoTempPath)
SkipFile = True
FileLocked = "No locks"
End If
End While
If Not (SkipFile) Then
My.Computer.FileSystem.CopyFile(IconFilePath, icoTempPath, True)
End If
FilesToDelete.Add(icoTempPath)
End If
@@ -95,7 +124,21 @@ Public Class RDP
'If Not LightExitCode = 0 Then Exit Sub
'Move MSI file to destination and delete temp files
My.Computer.FileSystem.MoveFile(msiPath, DestinationPath, True)
FileLocked = LockCheck.CheckLock(DestinationPath)
While Not (FileLocked = "No locks")
If (MessageBox.Show("The file " + DestinationPath + " is currently locked. Lock information:" + FileLocked + vbNewLine + "Do you want to try again?", "File Locked", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
FileLocked = LockCheck.CheckLock(DestinationPath)
Else
MessageBox.Show("The following file will not be deleted:" + vbNewLine + DestinationPath)
SkipFile = True
FileLocked = "No locks"
End If
End While
If Not (SkipFile) Then
My.Computer.FileSystem.MoveFile(msiPath, DestinationPath, True)
End If
DeleteFiles(FilesToDelete)
End Sub
@@ -122,7 +165,22 @@ Public Class RDP
Private Sub DeleteFiles(FilesArray As List(Of String))
For Each dFile In FilesArray
If My.Computer.FileSystem.FileExists(dFile) Then My.Computer.FileSystem.DeleteFile(dFile)
Dim LockCheck As New LockChecker.LockChecker()
Dim FileLocked As String
Dim SkipFile As Boolean = False
FileLocked = LockCheck.CheckLock(dFile)
While Not (FileLocked = "No locks")
If (MessageBox.Show("The file " + dFile + " is currently locked. Lock information:" + FileLocked + vbNewLine + "Do you want to try again?", "File Locked", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
FileLocked = LockCheck.CheckLock(dFile)
Else
MessageBox.Show("The following file will not be deleted:" + vbNewLine + dFile)
SkipFile = True
FileLocked = "No locks"
End If
End While
If Not (SkipFile) Then
If My.Computer.FileSystem.FileExists(dFile) Then My.Computer.FileSystem.DeleteFile(dFile)
End If
Next
End Sub

View File

@@ -46,6 +46,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -99,6 +100,12 @@
<LastGenOutput>Settings.Designer.vb</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LockChecker\LockChecker.vbproj">
<Project>{029c010d-728b-4b87-b54a-08b2bbf49bd7}</Project>
<Name>LockChecker</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.