Search code examples
c#.netwinformssharedshared-directory

How to read folder file on shared drive?


I'm newbie C# developer. I'm doing new project on windows application. And i want to read folder on shared drive then add to combobox. Could you help me or show your solution to do it.

Now, I'm just read folder from my drive only.

This is my code.

  System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("D:\\Data\\");
            System.IO.FileSystemInfo[] files = di.GetFileSystemInfos();
            ddlCompany.Items.AddRange(files);


Solution

  • Maybe something Like the below:

    DirectoryInfo dir = new DirectoryInfo(@"D:\Data\");
    DirectoryInfo[] dirs = dir.GetDirectories();
    FileInfo[] files = dir.GetFiles();
    
    foreach (FileInfo file in files)
    {
      ddlCompany.Items.Add(file);
    }
    

    However with this in mind I believe you will need to read from this address as a UNC path - \\SERVER\Data\

    So would be more like:

    DirectoryInfo dir = new DirectoryInfo(@"\\SERVER\Data\");
    DirectoryInfo[] dirs = dir.GetDirectories();
    FileInfo[] files = dir.GetFiles();
    
    foreach (FileInfo file in files)
    {
      ddlCompany.Items.Add(file);
    }
    

    Please note that this will pick up all files and folders in the location.

    Hope this helps.