AaTurpin.DriveManager 1.0.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package AaTurpin.DriveManager --version 1.0.0
                    
NuGet\Install-Package AaTurpin.DriveManager -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AaTurpin.DriveManager" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AaTurpin.DriveManager" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="AaTurpin.DriveManager" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AaTurpin.DriveManager --version 1.0.0
                    
#r "nuget: AaTurpin.DriveManager, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=AaTurpin.DriveManager&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=AaTurpin.DriveManager&version=1.0.0
                    
Install as a Cake Tool

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:

  1. Add the DriveManager.cs file to your project
  2. Add reference to System.Management.Automation.dll
  3. 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 drives
  • New-PSDrive -Persist - To create persistent drive mappings
  • Remove-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 Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.