Exemplo n.º 1
0
 /// <summary>Creates a new share.</summary>
 /// <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="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 shareName, string path, long maximumUsers, string description, NetworkShareTypes type)
 {
     return(CreateShare(null, shareName, path, maximumUsers, description, type));
 }
Exemplo n.º 2
0
        /// <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);
                };
            };
        }