AaTurpin.DriveManager
1.0.0
dotnet add package AaTurpin.DriveManager --version 1.0.0
NuGet\Install-Package AaTurpin.DriveManager -Version 1.0.0
<PackageReference Include="AaTurpin.DriveManager" Version="1.0.0" />
<PackageVersion Include="AaTurpin.DriveManager" Version="1.0.0" />
<PackageReference Include="AaTurpin.DriveManager" />
paket add AaTurpin.DriveManager --version 1.0.0
#r "nuget: AaTurpin.DriveManager, 1.0.0"
#addin nuget:?package=AaTurpin.DriveManager&version=1.0.0
#tool nuget:?package=AaTurpin.DriveManager&version=1.0.0
DriveManager
A .NET Framework 4.7.2 utility for managing network drives in Windows applications.
Overview
DriveManager is a static class library that provides methods for mapping, unmapping, and checking network drives in Windows environments. It leverages PowerShell commands internally to perform drive operations in a robust and reliable way.
Features
- Map network drives with persistent connections
- Map multiple drives efficiently in batch operations
- Check if drives are already mapped and remap if necessary
- Robust error handling for network-related issues
- Unmapping of drives with force option to handle in-use scenarios
- Detection of network drives vs local drives
- Comprehensive logging of all operations
Requirements
- .NET Framework 4.7.2 or higher
- System.Management.Automation (PowerShell) reference
- Windows operating system
Installation
NuGet Package
The easiest way to install DriveManager is via NuGet:
Install-Package AaTurpin.DriveManager
This package automatically installs the required dependencies:
- ConfigManager
- RunLog
Manual Installation
If you prefer manual installation:
- Add the DriveManager.cs file to your project
- Add reference to System.Management.Automation.dll
- Ensure ConfigManager and RunLog dependencies are available in your project
Usage Examples
Setting Up Logging
// Configure a logger first
var logger = new RunLog.LoggerConfiguration()
.WriteTo.Console(RunLog.LogLevel.Information)
.WriteTo.File("logs/drive_operations.log")
.CreateLogger();
// Set the logger for DriveManager
DriveManager.SetLogger(logger);
Mapping a Single Drive
// Map a network drive
bool success = DriveManager.MapDrive("Z:", @"\\server\share");
if (success)
{
Console.WriteLine("Drive Z: mapped successfully");
}
else
{
Console.WriteLine("Failed to map drive Z:");
}
Mapping Multiple Drives
// Create drive mapping configurations
var mappings = new List<ConfigManager.DriveMapping>
{
new ConfigManager.DriveMapping { DriveLetter = "Y:", UncPath = @"\\server\share1" },
new ConfigManager.DriveMapping { DriveLetter = "Z:", UncPath = @"\\server\share2" }
};
// Map all drives
bool allSuccessful = DriveManager.MapNetworkDrives(mappings);
Unmapping a Drive
// Unmap a network drive
bool success = DriveManager.UnmapDrive("Z:");
Checking if a Drive is a Network Drive
// Check if a drive is a network drive
if (DriveManager.IsNetworkDrive("Z:"))
{
Console.WriteLine("Z: is a network drive");
}
else
{
Console.WriteLine("Z: is a local drive");
}
Error Handling
DriveManager provides robust error handling for various network-related issues:
try
{
bool success = DriveManager.MapDrive("Z:", @"\\server\share");
// Process result
}
catch (Exception ex)
{
if (DriveManager.IsNetworkPathError(ex))
{
Console.WriteLine("Network path error - server may be unavailable");
}
else
{
Console.WriteLine($"Other error: {ex.Message}");
}
}
Integration with ConfigManager
DriveManager works seamlessly with the ConfigManager library to load drive mappings from configuration files:
// Get drive mappings from configuration
var driveMappings = ConfigManager.ConfigHelper.GetDriveMappings();
// Map all configured drives
DriveManager.MapNetworkDrives(driveMappings);
PowerShell Integration
DriveManager uses PowerShell cmdlets internally for maximum compatibility and robustness:
Get-PSDrive
- To check existing drivesNew-PSDrive -Persist
- To create persistent drive mappingsRemove-PSDrive -Force
- To remove drive mappings
This approach ensures that drive mappings are integrated with the Windows operating system and persist between sessions.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETFramework 4.7.2
- AaTurpin.ConfigManager (>= 1.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|
Initial Release.