2014-09-01 9 views
5

मैं एक नेटवर्क सर्वर से सभी साझा निर्देशिका की सूची बनाना चाहते से सभी साझा फ़ोल्डर्स।सूची किसी नेटवर्क स्थान

सूचीबद्ध करने के लिए एक साझा नेटवर्क निर्देशिका से निर्देशिका मैं इस्तेमाल किया

Directory.GetDirectories(@"\\server\share\") 

समस्या मैं \\server पर सभी फ़ोल्डर की सूची बनाना चाहते है।

अगर मैं एक ही विधि का उपयोग मैं एक अपवाद कह

यूएनसी पथ प्रपत्र \ सर्वर \ शेयर

मैं हर जगह देखा की होनी चाहिए हो और मैं नहीं कर सकता इसके लिए एक समाधान खोजें।

किसी को भी मैं क्या आदेश \\share में फ़ोल्डर प्रदर्शित करने के लिए क्या करना चाहिए की किसी भी विचार है?

+0

संभावित डुप्लिकेट [स्थानीय नेटवर्क सर्वर पर सभी यूएनसी साझा फ़ोल्डर्स की एक सूची प्राप्त करें] (http://stackoverflow.com/questions/3567063/get-a-list-of-all-unc-shared- फ़ोल्डर्स- ऑन-ए-स्थानीय नेटवर्क सर्वर) – kamaradclimber

उत्तर

1

सबसे अच्छा समाधान मैं मिल सकता है एक छिपा हुआ cmd.exe उदाहरण से "शुद्ध" एप्लिकेशन को कॉल करने के लिए किया गया था:

public static string[] GetDirectoriesInNetworkLocation(string networkLocationRootPath) 
{ 
    Process cmd = new Process(); 
    cmd.StartInfo.FileName = "cmd.exe"; 
    cmd.StartInfo.RedirectStandardInput = true; 
    cmd.StartInfo.RedirectStandardOutput = true; 
    cmd.StartInfo.CreateNoWindow = true; 
    cmd.StartInfo.UseShellExecute = false; 
    cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
    cmd.Start(); 
    cmd.StandardInput.WriteLine($"net view {networkLocationRootPath}"); 
    cmd.StandardInput.Flush(); 
    cmd.StandardInput.Close(); 

    string output = cmd.StandardOutput.ReadToEnd(); 

    cmd.WaitForExit(); 
    cmd.Close(); 

    output = output.Substring(output.LastIndexOf('-') + 2); 
    output = output.Substring(0, output.IndexOf("The command completed successfully.")); 

    return 
     output 
      .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) 
      .Select(x => System.IO.Path.Combine(networkLocationRootPath, x.Substring(0, x.IndexOf(' ')))) 
      .ToArray(); 
} 

आपके उपयोग के मामले के आधार पर आप networkLocationRootPath मान्य करने के लिए किसी भी बचना चाहें cmd इंजेक्शन मुद्दों।

0

यह codeproject.com के अनुसार .net के लापता हिस्सा प्रतीत होता है। वेबसाइट फिर भी एक solution कि 2003

में काम किया आप इसे कोशिश करते हैं और अगर यह काम करता है व्याख्या कर सकते हैं वर्णन करता है?

+0

यह वही है ओ पी चाहता है नहीं है। यह उन फ़ोल्डर्स को सूचीबद्ध करना है जो आपकी मशीन से साझा की जाती हैं। ओपी (और मैं) नेटवर्क पर किसी अन्य मशीन से साझा फ़ोल्डर सूचीबद्ध करना चाहता हूं। –

+1

मुझे वास्तव में ~ 3y ago से उत्तर पर टिप्पणी प्राप्त करने में खुशी हुई :) – kamaradclimber

+0

मैं इसे देख रहा था और यह उन प्रश्नों में से एक था जो आया था। मैंने अब एक समाधान के रूप में पाया समाधान जोड़ा है। –

2

मैं जानता हूँ कि इस सूत्र पुराना है, लेकिन इस समाधान अंत में किसी की मदद कर सकते हैं। मैंने एक कमांड लाइन का उपयोग किया, फिर उसके आउटपुट से निर्देशिका नाम वाले एक सबस्ट्रिंग को वापस कर दिया।

static void Main(string[] args) 
    { 
     string servername = "my_test_server"; 
     List<string> dirlist = getDirectories(servername); 
     foreach (var dir in dirlist) 
     { 
      Console.WriteLine(dir.ToString()); 
     }  
     Console.ReadLine(); 
    } 

    public static List<string> getDirectories (string servername) 
    { 
     Process cmd = new Process(); 
     cmd.StartInfo.FileName = "cmd.exe"; 
     cmd.StartInfo.RedirectStandardInput = true; 
     cmd.StartInfo.RedirectStandardOutput = true; 
     cmd.StartInfo.CreateNoWindow = true; 
     cmd.StartInfo.UseShellExecute = false; 
     cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     cmd.Start(); 
     cmd.StandardInput.WriteLine("net view \\\\" + servername); 
     cmd.StandardInput.Flush(); 
     cmd.StandardInput.Close(); 
     string output = cmd.StandardOutput.ReadToEnd(); 
     cmd.WaitForExit(); 
     cmd.Close(); 
     List<string> dirlist = new List<string>(); 
     if(output.Contains("Disk")) 
     { 
      int firstindex = output.LastIndexOf("-") + 1; 
      int lastindex = output.LastIndexOf("Disk"); 
      string substring = ((output.Substring(firstindex, lastindex - firstindex)).Replace("Disk", string.Empty).Trim()); 
      dirlist = substring.Split('\n').ToList(); 
     } 
     return dirlist; 
    } 
संबंधित मुद्दे