Skip to content

Commit

Permalink
.net 6, bump v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Sep 29, 2022
1 parent 4f67ad1 commit a2f44ff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
5 changes: 4 additions & 1 deletion CatalogTcx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>catalog_tcx</RootNamespace>
<Authors>Steven M. Cohn</Authors>
<Company>River Software</Company>
Expand All @@ -12,6 +12,9 @@
<PackageIcon>Garmin.ico</PackageIcon>
<PackageIconUrl />
<ApplicationIcon>assets\Garmin.ico</ApplicationIcon>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<Version>$(AssemblyVersion)</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>", Scope = "member", Target = "~M:CatalogTcx.UsbFactory.GetAvailableDisks~System.Collections.Generic.List{CatalogTcx.UsbDisk}")]
[assembly: SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>", Scope = "member", Target = "~M:CatalogTcx.WmiExtensions.First(System.Management.ManagementObjectSearcher)~System.Management.ManagementObject")]
[assembly: SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>", Scope = "member", Target = "~M:CatalogTcx.UsbFactory.GetAvailableDisks~System.Collections.Generic.IEnumerable{CatalogTcx.UsbDisk}")]
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static int ScanFiles(string sourcePath, string targetPath, bool clean)
private static int ScanGarmin (string path)
{
int count = 0;
List<UsbDisk> disks = null;
IEnumerable<UsbDisk> disks = null;

try
{
Expand All @@ -144,7 +144,7 @@ private static int ScanGarmin (string path)
return count;
}

if ((disks != null) && (disks.Count > 0))
if ((disks != null) && (disks.Any()))
{
var disk = disks.FirstOrDefault(
e => e.Model.StartsWith("Garmin", StringComparison.InvariantCulture));
Expand Down
63 changes: 27 additions & 36 deletions UsbFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,40 @@ internal class UsbFactory
/// </summary>
/// <returns>A List of UsbDisk instances.</returns>

public List<UsbDisk> GetAvailableDisks ()
public IEnumerable<UsbDisk> GetAvailableDisks()
{
var disks = new List<UsbDisk>();
using var searcher = new ManagementObjectSearcher(
"select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get();

using (var searcher = new ManagementObjectSearcher(
"select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get())
// browse all USB WMI physical disks
foreach (var o in searcher)
{
// browse all USB WMI physical disks
foreach (var o in searcher)
{
var drive = (ManagementObject) o;
// associate physical disks with partitions
using (var partition = new ManagementObjectSearcher(
$"associators of {{Win32_DiskDrive.DeviceID='{drive["DeviceID"]}'}} where AssocClass = Win32_DiskDriveToDiskPartition").First())
{
if (partition == null) continue;
var drive = (ManagementObject)o;

// associate partitions with logical disks (drive letter volumes)
using (var logical = new ManagementObjectSearcher(
$"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where AssocClass = Win32_LogicalDiskToPartition").First())
{
if (logical == null) continue;
// associate physical disks with partitions
using var partition = new ManagementObjectSearcher(
$"associators of {{Win32_DiskDrive.DeviceID='{drive["DeviceID"]}'}} where AssocClass = Win32_DiskDriveToDiskPartition").First();

// finally find the logical disk entry to determine the volume name
using (var volume = new ManagementObjectSearcher(
$"select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{logical["Name"]}'").First())
{
var disk = new UsbDisk(logical["Name"].ToString())
{
Model = drive["Model"].ToString(),
Volume = volume["VolumeName"].ToString(),
FreeSpace = (ulong) volume["FreeSpace"],
Size = (ulong) volume["Size"]
};
if (partition == null) continue;

disks.Add(disk);
}
}
}
}
}
// associate partitions with logical disks (drive letter volumes)
using var logical = new ManagementObjectSearcher(
$"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where AssocClass = Win32_LogicalDiskToPartition").First();

if (logical == null) continue;

return disks;
// finally find the logical disk entry to determine the volume name
using var volume = new ManagementObjectSearcher(
$"select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{logical["Name"]}'").First();

yield return new UsbDisk(logical["Name"].ToString())
{
Model = drive["Model"].ToString(),
Volume = volume["VolumeName"].ToString(),
FreeSpace = (ulong)volume["FreeSpace"],
Size = (ulong)volume["Size"]
};
}
}
}
}

0 comments on commit a2f44ff

Please sign in to comment.