How can I enumerate network shares and web folders in C#?

这一生的挚爱 提交于 2019-12-01 19:36:42

yes you can do this take a look at this site and it will show you how to do it in VB as well as C#

Enumerating Network Drives / Shares

here is a sample app you can try as well from codeproject

//

// Enumerate shares on local computer

//

Console.WriteLine("\nShares on local computer:");
ShareCollection shi = ShareCollection.LocalShares;
if (shi != null) 
{
    foreach(Share si in shi) 
    {
        Console.WriteLine("{0}: {1} [{2}]", 
            si.ShareType, si, si.Path);

        // If this is a file-system share, try to

        // list the first five subfolders.

        // NB: If the share is on a removable device,

        // you could get "Not ready" or "Access denied"

        // exceptions.

        if (si.IsFileSystem) 
        {
            try 
            {
                System.IO.DirectoryInfo d = si.Root;
                System.IO.DirectoryInfo[] Flds = d.GetDirectories();
                for (int i=0; i < Flds.Length && i < 5; i++)
                    Console.WriteLine("\t{0} - {1}", i, Flds[i].FullName);

                Console.WriteLine();
            }
            catch (Exception ex) 
            {
                Console.WriteLine("\tError listing {0}:\n\t{1}\n", 
                    si, ex.Message);
            }
        }
    }
}
else
    Console.WriteLine("Unable to enumerate the local shares.");

//

// Resolve local paths to UNC paths.

//

Console.WriteLine("{0} = {1}", 
    fileName, ShareCollection.PathToUnc(fileName));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!