OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to mount FTP folder as local disk

  • Thread starter Thread starter Hugo Jiménez
  • Start date Start date
H

Hugo Jiménez

Guest
I'm trying to mount an FTP network folder as a local drive, similar to how the Google Drive application mounts its network drive

This is my first time working with FTP and network connections.

Reading other answers and forums I have the following NuGet packages installed:

  • Cake.Powershell
  • FluentFTP
  • Microsoft.Management.Infrastructure

This is my code:

Code:
private void bAnadir_Click(object sender, EventArgs e)
{
    string remotePath = @"ftpPath";

    string username = "user";
    string password = "paswrd";

    char driveLetter = 'S';

    driveLetter = FirstAvailableLetter(driveLetter);

    MountFTP(remotePath, username, password, driveLetter);

    MessageBox.Show($"FTP '{remotePath}' was mounted as drive {driveLetter}: successfully.");
}

private void MountFTP(string ftpPath, string username, string password, char driveLetter)
{
    string cmd = $"New-PSDrive -Name {driveLetter} -PSProvider FileSystem -Root {ftpPath} -Credential (New-Object System.Management.Automation.PSCredential('{username}', (ConvertTo-SecureString '{password}' -AsPlainText -Force)))";
    RunPowerShellCommand(cmd);
}

private void RunPowerShellCommand(string command)
{
    using (PowerShell PowerShellInstance = PowerShell.Create())
    {
        PowerShellInstance.AddScript(command);

        WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
        PowerShellInstance.Runspace = RunspaceFactory.CreateRunspace(connectionInfo);

        PowerShellInstance.Runspace.Open();

        Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

        foreach (PSObject outputItem in PSOutput)
        {
            if (outputItem != null)
            {
                Console.WriteLine(outputItem.BaseObject.GetType().FullName);
                Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
            }
        }

        if (PowerShellInstance.Streams.Error.Count > 0)
        {
            foreach (ErrorRecord error in PowerShellInstance.Streams.Error)
            {
                MessageBox.Show($"Error: {error.Exception.Message}");
            }
        }
    }
}

static bool IsLetterAvailable(char driveLetter)
{
    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo drive in allDrives)
    {
        if (drive.IsReady && string.Equals(drive.Name.Substring(0, 1), driveLetter.ToString(), StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }
    }
    return true;
}

static char FirstAvailableLetter(char preferredDriveLetter)
{
    char driveLetter = preferredDriveLetter;

    if (IsLetterAvailable(driveLetter))
    {
        return driveLetter;
    }

    for (char letter = 'D'; letter <= 'Z'; letter++)
    {
        if (IsLetterAvailable(letter))
        {
            return letter;
        }
    }

    return '0';
}

Thanks a lot!!
<p>I'm trying to mount an FTP network folder as a local drive, similar to how the Google Drive application mounts its network drive</p>
<p>This is my first time working with FTP and network connections.</p>
<p>Reading other answers and forums I have the following NuGet packages installed:</p>
<ul>
<li><strong>Cake.Powershell</strong></li>
<li><strong>FluentFTP</strong></li>
<li><strong>Microsoft.Management.Infrastructure</strong></li>
</ul>
<p>This is my code:</p>
<pre><code>private void bAnadir_Click(object sender, EventArgs e)
{
string remotePath = @"ftpPath";

string username = "user";
string password = "paswrd";

char driveLetter = 'S';

driveLetter = FirstAvailableLetter(driveLetter);

MountFTP(remotePath, username, password, driveLetter);

MessageBox.Show($"FTP '{remotePath}' was mounted as drive {driveLetter}: successfully.");
}

private void MountFTP(string ftpPath, string username, string password, char driveLetter)
{
string cmd = $"New-PSDrive -Name {driveLetter} -PSProvider FileSystem -Root {ftpPath} -Credential (New-Object System.Management.Automation.PSCredential('{username}', (ConvertTo-SecureString '{password}' -AsPlainText -Force)))";
RunPowerShellCommand(cmd);
}

private void RunPowerShellCommand(string command)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript(command);

WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
PowerShellInstance.Runspace = RunspaceFactory.CreateRunspace(connectionInfo);

PowerShellInstance.Runspace.Open();

Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
Console.WriteLine(outputItem.BaseObject.GetType().FullName);
Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
}
}

if (PowerShellInstance.Streams.Error.Count > 0)
{
foreach (ErrorRecord error in PowerShellInstance.Streams.Error)
{
MessageBox.Show($"Error: {error.Exception.Message}");
}
}
}
}

static bool IsLetterAvailable(char driveLetter)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo drive in allDrives)
{
if (drive.IsReady && string.Equals(drive.Name.Substring(0, 1), driveLetter.ToString(), StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
return true;
}

static char FirstAvailableLetter(char preferredDriveLetter)
{
char driveLetter = preferredDriveLetter;

if (IsLetterAvailable(driveLetter))
{
return driveLetter;
}

for (char letter = 'D'; letter <= 'Z'; letter++)
{
if (IsLetterAvailable(letter))
{
return letter;
}
}

return '0';
}
</code></pre>
<p>Thanks a lot!!</p>
Continue reading...
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Ads by Eonads
Top