/// <summary>Gets all the shares defined on the machine.</summary> /// <param name="machineName">The name of the machine to enumerate. If <see langword="null"/> or /// empty then the current machine is used.</param> /// <returns>An array of shares.</returns> /// <exception cref="ArgumentException"><paramref name="machineName"/> is invalid.</exception> /// <exception cref="UnauthorizedAccessException">The user does not have permission to perform the operation.</exception> /// <exception cref="ShareException">An error occurred performing the operation.</exception> public static NetworkShareInfo[] GetShares(string machineName) { NetworkShareInfo[] shares = null; //Open the WMI server ManagementScope scope = OpenWMI(machineName); //Retrieve all the shares ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Share"); using (ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query)) { //Enumerate the shares using (ManagementObjectCollection results = search.Get()) { if (results != null) { shares = new NetworkShareInfo[results.Count]; int nIdx = 0; foreach (ManagementObject result in results) { shares[nIdx++] = MakeShare(machineName, result); } ; } ; }; }; return(shares ?? new NetworkShareInfo[0]); }
/// <summary>Refreshes the share properties.</summary> public void Refresh() { //Retrieve new values NetworkShareInfo info = NetworkShare.GetShare(Name); //Copy them m_strDescription = info.Description; m_strPath = info.Path; m_Type = info.ShareType; m_UsersMax = info.MaximumUsers; }
private static NetworkShareInfo MakeShare(string machineName, ManagementObject result) { NetworkShareInfo share = new NetworkShareInfo(machineName); //Copy the data share.Description = result["Description"] as string; if (!Convert.ToBoolean(result["AllowMaximum"])) { share.MaximumUsers = Convert.ToUInt32(result["MaximumAllowed"]); } share.Name = result["Name"] as string; share.Path = result["Path"] as string; share.ShareType = (NetworkShareTypes)Convert.ToUInt32(result["Type"]); return(share); }
/// <summary>Creates a new share.</summary> /// <param name="machineName">The machine on which to create the share. If empty or <see langword="null"/> then /// the current machine is used.</param> /// <param name="shareName">The name of the share to create.</param> /// <param name="path">The local path of the share.</param> /// <param name="maximumUsers">The maximum number of simultaneous users allowed on the share. Set to 0xFFFFFFFF to set no limit.</param> /// <param name="description">The description of the share.</param> /// <param name="type">The type of the share.</param> /// <exception cref="ArgumentNullException"><paramref name="shareName"/> or <paramref name="path"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException"> /// <paramref name="shareName"/> or <paramref name="path"/> is empty or invalid. /// <para>-or-</para> /// <paramref name="machineName"/> is invalid. /// <para>-or-</para> /// <paramref name="type"/> specifies an administrative share. /// </exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="maximumUsers"/> is less than -1 or greater than Int32.MaxValue.</exception> /// <exception cref="DirectoryNotFoundException">The path is invalid or does not exist for the specified type.</exception> /// <exception cref="UnauthorizedAccessException">The user does not have permission to perform the operation.</exception> /// <exception cref="ShareAlreadyExistsException">A share already exists with the given name.</exception> /// <exception cref="ShareException">An error occurred performing the operation.</exception> public static NetworkShareInfo CreateShare(string machineName, string shareName, string path, long maximumUsers, string description, NetworkShareTypes type) { //Validation shareName = ValidateShareName(shareName); path = ValidationHelper.ThrowIfArgumentStringEmpty(path, "path"); description = (description != null) ? description.Trim() : ""; if ((type & NetworkShareTypes.Administrative) == NetworkShareTypes.Administrative) { throw new ArgumentException("Administrative shares can not be created."); } ValidationHelper.ThrowIfArgumentOutOfRange((int)maximumUsers, "maximumUsers", -1, Int32.MaxValue); //Open a connection ManagementScope scope = OpenWMI(machineName); //Get the class using (ManagementClass cls = new ManagementClass()) { cls.Scope = scope; cls.ClassPath.ClassName = "Win32_Share"; //Create the object using (ManagementObject inst = cls.CreateInstance()) { //Create the instance int returnCode = Convert.ToInt32(inst.InvokeMethod("Create", new object[] { path, shareName, type, maximumUsers, description, null, null })); if (returnCode != 0) { throw CreateException(shareName, returnCode); } //Return the new instance NetworkShareInfo share = new NetworkShareInfo(machineName); share.Description = description; share.MaximumUsers = maximumUsers; share.Name = shareName; share.Path = path; share.ShareType = type; return(share); }; }; }