public static void LoadGlobalShares(SharesList dstList) { // There are multiple ways actually: // 1. Lounch the "net share" command // 2. Read shares from registry // 3. Call native methods from mpr.dll // 4. System.Management // Side note: actually there's something like Win32_ShareToDirectory in Windows, // but I already have all the nice abstraction around having a share list... using (ManagementClass exportedShares = new ManagementClass("Win32_Share")) { ManagementObjectCollection shares = exportedShares.GetInstances(); List <Share> addWithHigherPriority = new List <Share>(shares.Count); List <Share> addWithLowerPriority = new List <Share>(shares.Count); foreach (ManagementObject share in shares) { // Reference: https://msdn.microsoft.com/en-us/library/aa394435.aspx ShareType type = (ShareType)Convert.ToUInt32(share["Type"]); string shareName = share["Name"].ToString(); string localPath = share["Path"].ToString(); if (type == ShareType.DiskDrive || type == ShareType.Device) { addWithHigherPriority.Add(new Share(shareName, new TokenizedLocalPath(localPath, '\\'))); } else if (type == ShareType.DiskDriveAdmin || type == ShareType.DeviceAdmin) { // Take it, but add it with a lower priority. // This way the explicit shares take priority, as these are usually // the ones the user wants to be used and often have lesser // access restrictions. addWithLowerPriority.Add(new Share(shareName, new TokenizedLocalPath(localPath, '\\'))); } else { // skip everything else } } // First add the shares that should take lower priority. foreach (Share share in addWithLowerPriority) { dstList.AddOrReplace(share); } // Then add the ones with higher priority, overwriting eventual shares. foreach (Share share in addWithHigherPriority) { dstList.AddOrReplace(share); } } }
public static void ParseSmbConfShareList(SharesList dstList, string smbConfContent) { // note: some smb.conffeatures are not handled. Like special variables and includes. // TODO special case for [homes] share var parser = new IniDataParser(); parser.Configuration.CommentRegex = new System.Text.RegularExpressions.Regex(@"^[#;](.*)"); var iniData = parser.Parse(smbConfContent); foreach (var shareIniSection in iniData.Sections) { string shareName = shareIniSection.SectionName; if (shareName == "global") { continue; } if (!shareIniSection.Keys.ContainsKey("path")) { throw new Exception(String.Format("share {0} doesn't have local path specified", shareName)); } string shareLocalPath = shareIniSection.Keys["path"]; dstList.AddOrReplace(new Share(shareName, new TokenizedLocalPath(shareLocalPath, '/'))); } }
public static void ParseNetUserShareList(SharesList dstList, string shareListIniContent) { var parser = new IniDataParser(); var iniData = parser.Parse(shareListIniContent); foreach (var shareIniSection in iniData.Sections) { string shareName = shareIniSection.SectionName; if (!shareIniSection.Keys.ContainsKey("path")) { throw new Exception(String.Format("share {0} doesn't have local path specified", shareName)); } string shareLocalPath = shareIniSection.Keys["path"]; dstList.AddOrReplace(new Share(shareName, new TokenizedLocalPath(shareLocalPath, '/'))); } }