Skip to content

Commit ccd0374

Browse files
Randall FlaggRandall Flagg
Randall Flagg
authored and
Randall Flagg
committed
Updated the code to .net 8
1 parent 978c814 commit ccd0374

File tree

7 files changed

+1098
-1990
lines changed

7 files changed

+1098
-1990
lines changed
Lines changed: 15 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,26 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProductVersion>9.0.21022</ProductVersion>
7-
<SchemaVersion>2.0</SchemaVersion>
8-
<ProjectGuid>{6C463EA8-FE3F-4832-B22B-E1B199F2C308}</ProjectGuid>
9-
<OutputType>Library</OutputType>
10-
<AppDesignerFolder>Properties</AppDesignerFolder>
3+
<TargetFramework>net8.0</TargetFramework>
114
<RootNamespace>FileLockFinder</RootNamespace>
125
<AssemblyName>FileLockFinder</AssemblyName>
13-
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
14-
<FileAlignment>512</FileAlignment>
15-
<FileUpgradeFlags>
16-
</FileUpgradeFlags>
17-
<UpgradeBackupLocation>
18-
</UpgradeBackupLocation>
19-
<OldToolsVersion>3.5</OldToolsVersion>
20-
<TargetFrameworkProfile />
21-
</PropertyGroup>
22-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
23-
<DebugSymbols>true</DebugSymbols>
24-
<DebugType>full</DebugType>
25-
<Optimize>false</Optimize>
26-
<OutputPath>..\..\bin\Debug\plugins\</OutputPath>
27-
<DefineConstants>DEBUG;TRACE</DefineConstants>
28-
<ErrorReport>prompt</ErrorReport>
29-
<WarningLevel>4</WarningLevel>
30-
<Prefer32Bit>false</Prefer32Bit>
31-
</PropertyGroup>
32-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
33-
<DebugType>pdbonly</DebugType>
34-
<Optimize>true</Optimize>
35-
<OutputPath>..\..\bin\Release\plugins\</OutputPath>
36-
<DefineConstants>TRACE</DefineConstants>
37-
<ErrorReport>prompt</ErrorReport>
38-
<WarningLevel>4</WarningLevel>
39-
<Prefer32Bit>false</Prefer32Bit>
40-
</PropertyGroup>
41-
<PropertyGroup>
6+
<Title>FileLockFinder</Title>
7+
<OutputType>Library</OutputType>
428
<SignAssembly>true</SignAssembly>
43-
</PropertyGroup>
44-
<PropertyGroup>
459
<AssemblyOriginatorKeyFile>..\Solution Items\Key.snk</AssemblyOriginatorKeyFile>
10+
<OutputPath>$(SolutionDir)..\bin\$(Configuration)\plugins</OutputPath>
11+
<DefineConstants>$(DefineConstants)</DefineConstants>
4612
</PropertyGroup>
47-
<ItemGroup>
48-
<Reference Include="System" />
49-
<Reference Include="System.Data" />
50-
<Reference Include="System.Drawing" />
51-
<Reference Include="System.Windows.Forms" />
52-
</ItemGroup>
53-
<ItemGroup>
54-
<Compile Include="..\Solution Items\AssemblyVersion.cs">
55-
<Link>Properties\AssemblyVersion.cs</Link>
56-
</Compile>
57-
<Compile Include="LockFinder.cs" />
58-
<Compile Include="Properties\AssemblyInfo.cs" />
59-
</ItemGroup>
13+
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
15+
<Optimize>False</Optimize>
16+
</PropertyGroup>
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
18+
<Optimize>True</Optimize>
19+
</PropertyGroup>
20+
6021
<ItemGroup>
6122
<None Include="..\Solution Items\Key.snk">
6223
<Link>Key.snk</Link>
6324
</None>
6425
</ItemGroup>
65-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
66-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
67-
Other similar extension points exist, see Microsoft.Common.targets.
68-
<Target Name="BeforeBuild">
69-
</Target>
70-
<Target Name="AfterBuild">
71-
</Target>
72-
-->
73-
<PropertyGroup>
74-
<PostBuildEvent>
75-
</PostBuildEvent>
76-
</PropertyGroup>
77-
</Project>
26+
</Project>

src/FileLockFinder/LockFinder.cs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ public class LockFinder
2626
static public string FindLockedProcessName(string path)
2727
{
2828
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+
}
3134
return list[0].ProcessName;
3235
}
3336

@@ -41,7 +44,7 @@ static public string FindLockedProcessName(string path)
4144
static public bool CheckIfFileIsLocked(string path)
4245
{
4346
var list = FindLockProcesses(path);
44-
if(list.Count > 0) { return true; }
47+
if (list.Count > 0) { return true; }
4548
return false;
4649
}
4750

@@ -55,11 +58,10 @@ static public bool CheckIfFileIsLocked(string path)
5558
/// <exception cref="Exception"></exception>
5659
static public List<Process> FindLockProcesses(string path)
5760
{
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>();
6163

62-
int res = RmStartSession(out handle, 0, key);
64+
int res = RmStartSession(out uint handle, 0, key);
6365
if (res != 0)
6466
{
6567
throw new Exception("Could not begin restart session. " +
@@ -68,27 +70,26 @@ static public List<Process> FindLockProcesses(string path)
6870

6971
try
7072
{
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];
7576

7677
res = RmRegisterResources(handle, (uint)resources.Length,
7778
resources, 0, null, 0, null);
7879
if (res != 0)
7980
{
8081
throw new Exception("Could not register resource.");
8182
}
82-
res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null,
83+
res = RmGetList(handle, out uint pnProcInfoNeeded, ref pnProcInfo, null,
8384
ref lpdwRebootReasons);
85+
const int ERROR_MORE_DATA = 234;
8486
if (res == ERROR_MORE_DATA)
8587
{
8688
RM_PROCESS_INFO[] processInfo =
8789
new RM_PROCESS_INFO[pnProcInfoNeeded];
8890
pnProcInfo = pnProcInfoNeeded;
8991
// 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);
9293
if (res == 0)
9394
{
9495
processes = new List<Process>((int)pnProcInfo);
@@ -115,11 +116,11 @@ static public List<Process> FindLockProcesses(string path)
115116
}
116117
catch (Exception exception)
117118
{
118-
Console.WriteLine(exception.Message);
119+
Trace.WriteLine(exception.Message);
119120
}
120121
finally
121122
{
122-
RmEndSession(handle);
123+
Trace.WriteLine($"RmEndSession: {RmEndSession(handle)}");
123124
}
124125

125126
return processes;
@@ -135,22 +136,22 @@ struct RM_UNIQUE_PROCESS
135136
public System.Runtime.InteropServices.
136137
ComTypes.FILETIME ProcessStartTime;
137138
}
138-
[DllImport("rstrtmgr.dll",
139+
[DllImport("rstrtmgr.dll",
139140
CharSet = CharSet.Auto, SetLastError = true)]
140-
static extern int RmGetList(uint dwSessionHandle,
141+
static extern int RmGetList(uint dwSessionHandle,
141142
out uint pnProcInfoNeeded,
142143
ref uint pnProcInfo,
143144
[In, Out] RM_PROCESS_INFO[] rgAffectedApps,
144145
ref uint lpdwRebootReasons);
145-
[StructLayout(LayoutKind.Sequential,
146+
[StructLayout(LayoutKind.Sequential,
146147
CharSet = CharSet.Auto)]
147148
struct RM_PROCESS_INFO
148149
{
149150
public RM_UNIQUE_PROCESS Process;
150-
[MarshalAs(UnmanagedType.ByValTStr,
151+
[MarshalAs(UnmanagedType.ByValTStr,
151152
SizeConst = CCH_RM_MAX_APP_NAME + 1)]
152153
public string strAppName;
153-
[MarshalAs(UnmanagedType.ByValTStr,
154+
[MarshalAs(UnmanagedType.ByValTStr,
154155
SizeConst = CCH_RM_MAX_SVC_NAME + 1)]
155156
public string strServiceShortName;
156157
public RM_APP_TYPE ApplicationType;
@@ -171,28 +172,22 @@ enum RM_APP_TYPE
171172
RmCritical = 1000
172173
}
173174

174-
[DllImport("rstrtmgr.dll",
175-
CharSet = CharSet.Auto,
176-
SetLastError = true)]
175+
[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto, SetLastError = true)]
177176
static extern int RmRegisterResources(
178177
uint pSessionHandle,
179-
UInt32 nFiles,
178+
UInt32 nFiles,
180179
string[] rgsFilenames,
181180
UInt32 nApplications,
182181
[In] RM_UNIQUE_PROCESS[] rgApplications,
183182
UInt32 nServices, string[] rgsServiceNames);
184183

185-
[DllImport("rstrtmgr.dll",
186-
CharSet = CharSet.Auto,
187-
SetLastError = true)]
184+
[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto, SetLastError = true)]
188185
static extern int RmStartSession(
189-
out uint pSessionHandle,
186+
out uint pSessionHandle,
190187
int dwSessionFlags,
191188
string strSessionKey);
192189

193-
[DllImport("rstrtmgr.dll",
194-
CharSet = CharSet.Auto,
195-
SetLastError = true)]
190+
[DllImport("rstrtmgr.dll", CharSet = CharSet.Auto, SetLastError = true)]
196191
static extern int RmEndSession(uint pSessionHandle);
197192
}
198193
}

src/FileLockFinder/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)