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

前端 未结 1 1242
太阳男子
太阳男子 2021-01-19 15:54

.Net provides us with a FolderBrowserDialog control to browse for folders. This is however a modal dialog box. I need to create a user control that I can drop onto my form.

相关标签:
1条回答
  • 2021-01-19 16:34

    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));
    
    0 讨论(0)
提交回复
热议问题