@@ -26,8 +26,11 @@ public class LockFinder
26
26
static public string FindLockedProcessName ( string path )
27
27
{
28
28
var list = FindLockProcesses ( path ) ;
29
- if ( list . Count == 0 ) { throw new Exception (
30
- "No processes are locking the path specified" ) ; }
29
+ if ( list . Count == 0 )
30
+ {
31
+ throw new Exception (
32
+ "No processes are locking the path specified" ) ;
33
+ }
31
34
return list [ 0 ] . ProcessName ;
32
35
}
33
36
@@ -41,7 +44,7 @@ static public string FindLockedProcessName(string path)
41
44
static public bool CheckIfFileIsLocked ( string path )
42
45
{
43
46
var list = FindLockProcesses ( path ) ;
44
- if ( list . Count > 0 ) { return true ; }
47
+ if ( list . Count > 0 ) { return true ; }
45
48
return false ;
46
49
}
47
50
@@ -55,11 +58,10 @@ static public bool CheckIfFileIsLocked(string path)
55
58
/// <exception cref="Exception"></exception>
56
59
static public List < Process > FindLockProcesses ( string path )
57
60
{
58
- uint handle ;
59
- string key = Guid . NewGuid ( ) . ToString ( ) ;
60
- List < Process > processes = new List < Process > ( ) ;
61
+ var key = Guid . NewGuid ( ) . ToString ( ) ;
62
+ var processes = new List < Process > ( ) ;
61
63
62
- int res = RmStartSession ( out handle , 0 , key ) ;
64
+ int res = RmStartSession ( out uint handle , 0 , key ) ;
63
65
if ( res != 0 )
64
66
{
65
67
throw new Exception ( "Could not begin restart session. " +
@@ -68,27 +70,26 @@ static public List<Process> FindLockProcesses(string path)
68
70
69
71
try
70
72
{
71
- const int ERROR_MORE_DATA = 234 ;
72
- uint pnProcInfoNeeded = 0 , pnProcInfo = 0 ,
73
- lpdwRebootReasons = RmRebootReasonNone ;
74
- string [ ] resources = new string [ ] { path } ;
73
+ uint pnProcInfo = 0 ;
74
+ uint lpdwRebootReasons = RmRebootReasonNone ;
75
+ string [ ] resources = [ path ] ;
75
76
76
77
res = RmRegisterResources ( handle , ( uint ) resources . Length ,
77
78
resources , 0 , null , 0 , null ) ;
78
79
if ( res != 0 )
79
80
{
80
81
throw new Exception ( "Could not register resource." ) ;
81
82
}
82
- res = RmGetList ( handle , out pnProcInfoNeeded , ref pnProcInfo , null ,
83
+ res = RmGetList ( handle , out uint pnProcInfoNeeded , ref pnProcInfo , null ,
83
84
ref lpdwRebootReasons ) ;
85
+ const int ERROR_MORE_DATA = 234 ;
84
86
if ( res == ERROR_MORE_DATA )
85
87
{
86
88
RM_PROCESS_INFO [ ] processInfo =
87
89
new RM_PROCESS_INFO [ pnProcInfoNeeded ] ;
88
90
pnProcInfo = pnProcInfoNeeded ;
89
91
// Get the list.
90
- res = RmGetList ( handle , out pnProcInfoNeeded , ref pnProcInfo ,
91
- processInfo , ref lpdwRebootReasons ) ;
92
+ res = RmGetList ( handle , out pnProcInfoNeeded , ref pnProcInfo , processInfo , ref lpdwRebootReasons ) ;
92
93
if ( res == 0 )
93
94
{
94
95
processes = new List < Process > ( ( int ) pnProcInfo ) ;
@@ -115,11 +116,11 @@ static public List<Process> FindLockProcesses(string path)
115
116
}
116
117
catch ( Exception exception )
117
118
{
118
- Console . WriteLine ( exception . Message ) ;
119
+ Trace . WriteLine ( exception . Message ) ;
119
120
}
120
121
finally
121
122
{
122
- RmEndSession ( handle ) ;
123
+ Trace . WriteLine ( $ " RmEndSession: { RmEndSession ( handle ) } " ) ;
123
124
}
124
125
125
126
return processes ;
@@ -135,22 +136,22 @@ struct RM_UNIQUE_PROCESS
135
136
public System . Runtime . InteropServices .
136
137
ComTypes . FILETIME ProcessStartTime ;
137
138
}
138
- [ DllImport ( "rstrtmgr.dll" ,
139
+ [ DllImport ( "rstrtmgr.dll" ,
139
140
CharSet = CharSet . Auto , SetLastError = true ) ]
140
- static extern int RmGetList ( uint dwSessionHandle ,
141
+ static extern int RmGetList ( uint dwSessionHandle ,
141
142
out uint pnProcInfoNeeded ,
142
143
ref uint pnProcInfo ,
143
144
[ In , Out ] RM_PROCESS_INFO [ ] rgAffectedApps ,
144
145
ref uint lpdwRebootReasons ) ;
145
- [ StructLayout ( LayoutKind . Sequential ,
146
+ [ StructLayout ( LayoutKind . Sequential ,
146
147
CharSet = CharSet . Auto ) ]
147
148
struct RM_PROCESS_INFO
148
149
{
149
150
public RM_UNIQUE_PROCESS Process ;
150
- [ MarshalAs ( UnmanagedType . ByValTStr ,
151
+ [ MarshalAs ( UnmanagedType . ByValTStr ,
151
152
SizeConst = CCH_RM_MAX_APP_NAME + 1 ) ]
152
153
public string strAppName ;
153
- [ MarshalAs ( UnmanagedType . ByValTStr ,
154
+ [ MarshalAs ( UnmanagedType . ByValTStr ,
154
155
SizeConst = CCH_RM_MAX_SVC_NAME + 1 ) ]
155
156
public string strServiceShortName ;
156
157
public RM_APP_TYPE ApplicationType ;
@@ -171,28 +172,22 @@ enum RM_APP_TYPE
171
172
RmCritical = 1000
172
173
}
173
174
174
- [ DllImport ( "rstrtmgr.dll" ,
175
- CharSet = CharSet . Auto ,
176
- SetLastError = true ) ]
175
+ [ DllImport ( "rstrtmgr.dll" , CharSet = CharSet . Auto , SetLastError = true ) ]
177
176
static extern int RmRegisterResources (
178
177
uint pSessionHandle ,
179
- UInt32 nFiles ,
178
+ UInt32 nFiles ,
180
179
string [ ] rgsFilenames ,
181
180
UInt32 nApplications ,
182
181
[ In ] RM_UNIQUE_PROCESS [ ] rgApplications ,
183
182
UInt32 nServices , string [ ] rgsServiceNames ) ;
184
183
185
- [ DllImport ( "rstrtmgr.dll" ,
186
- CharSet = CharSet . Auto ,
187
- SetLastError = true ) ]
184
+ [ DllImport ( "rstrtmgr.dll" , CharSet = CharSet . Auto , SetLastError = true ) ]
188
185
static extern int RmStartSession (
189
- out uint pSessionHandle ,
186
+ out uint pSessionHandle ,
190
187
int dwSessionFlags ,
191
188
string strSessionKey ) ;
192
189
193
- [ DllImport ( "rstrtmgr.dll" ,
194
- CharSet = CharSet . Auto ,
195
- SetLastError = true ) ]
190
+ [ DllImport ( "rstrtmgr.dll" , CharSet = CharSet . Auto , SetLastError = true ) ]
196
191
static extern int RmEndSession ( uint pSessionHandle ) ;
197
192
}
198
193
}
0 commit comments