Click here to Skip to main content
15,886,592 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have a Synology NAS for file storage and I want to use VB.net for automating things related to downloading and uploading files to and from the NAS server. I wish anyone could point me to a relevant resource as I have been trying hard to find a solution to this problem but can't seem to get any luck. Looking forward to your help.

What I have tried:

Tried using WebClient in .NET but can't find a solution getting it right as it seems IT only deals with an actual directory on my current file server but not on a NAS Server.
Posted
Updated 30-Jun-22 19:00pm

1 solution

Have you tried mapping the NAS folders to a virtual drive in Windows? It then appears as a "normal" drive letter to all apps and can be accessed as such.

I do that, but since my NASes are SMB1, Windows can be finicky about reconnecting them, so I wrote an app to remake the connections. (Even then, I have to restart Windows sometimes before it'll work - Windows really doesn't want you yo use SMB1 any more.)

The code is C#, but it's pretty obvious - it uses the "net use" windows cmd app to remove and remake a connection as this was the most reliable way I found (previously working WIN32 calls failed in Win10 and Win11 when SMB1 support was depreciated):
C#
/// <summary>
/// Tries to establish a connection to a network drive
/// </summary>
/// <param name="drive"></param>
/// <param name="path"></param>
private static void Connect(string drive, string path)
    {
    for (int attempts = 3; attempts > 0; attempts--)
        {
        if (attempts != 3) Thread.Sleep(5000);
        Process process = new Process();
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.FileName = "net";
        process.StartInfo.Arguments = $"use {drive} /delete";
        process.Start();
        process.WaitForExit();

        process = new Process();
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.FileName = "net";
        process.StartInfo.Arguments = $"use {drive} {path}";
        process.Start();
        process.WaitForExit();
        DriveInfo di = new DriveInfo(drive.Substring(0, 1));
        if (di.IsReady) return;
        }
    Storage.WriteLog($"Unable to connect {drive} to \"{path}\" after three attempts");
    }
 
Share this answer
 
Comments
Sys Dev 1-Jul-22 1:03am    
Thanks mate, you're a real hero. Will try this one :)
Sys Dev 1-Jul-22 1:07am    
Ughh, I forgot some important details to consider : my Database server is cloud-hosted and my NAS server is on premise. And my app's end users who'll download and upload files are from different locations around my country.
OriginalGriff 1-Jul-22 1:20am    
That gets more complicated, as your local IP address could be either static or dynamic depending on how much you pay your ISP - static is normally an extra cost, but that's what you would need for remote sites to access your NAS if it was shared across the internet.
This may help: https://www.google.com/search?q=map+drive+to+cloud+folder&oq=map+drive+to+cloud+folder&aqs=chrome..69i57j0i546l4j69i64.7648j0j7&sourceid=chrome&ie=UTF-8
But I haven't tried it: the security issue is far too great for me to want to do that, but it would depend on what you are storing / sharing. Do remember that exposing data to the internet means that GDPR applies so you'll need to be pretty careful how you secure things if you share a whole folder, even if you don't know exactly what your legitimate users are storing / accessing!
Sys Dev 1-Jul-22 1:25am    
I am also reluctant to try it but my database size is already eating up the remaining drive space on my server. In about 3 months it will consume all free disk space even if I truncate my database log file to free some space up. but thanks for this recommendation I will definitely check it out :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900