コード例 #1
0
 private static void ShareFolder(ILogger logger, DirectoryPath directoryPath, ShareName shareName, string shareDescription)
 {
     logger.LogDebug(string.Format("Starts trying to share {0} as {1}", directoryPath, shareName));
     WindowsShare.MethodStatus methodStatus = WindowsShare.Create(directoryPath.PathString, shareName.ToString(), WindowsShare.ShareType.DiskDrive, null, shareDescription, null);
     if (methodStatus != WindowsShare.MethodStatus.Success)
     {
         throw new ShareException(string.Format("Creating share failed for {0} at {1}. Error {2}.", shareName, directoryPath, methodStatus.ToString()));
     }
     logger.LogInfo(string.Format("Share {0} created for {1}.", shareName, directoryPath));
 }
コード例 #2
0
        public static IList <WindowsShare> GetAllShares()
        {
            IList <WindowsShare>       result = new List <WindowsShare>();
            ManagementClass            mc     = new ManagementClass("Win32_Share");
            ManagementObjectCollection moc    = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                WindowsShare share = new WindowsShare(mo);
                result.Add(share);
            }

            return(result);
        }
コード例 #3
0
        private static void SharePermissions(ILogger logger, ShareName shareName, string domain, string user, WindowsShare.AccessMaskTypes accessMask)
        {
            logger.LogDebug(string.Format("Trying to set permissions to share {0} for user {1}\\{2}", shareName, domain, user));
            WindowsShare windowsShare = WindowsShare.GetShareByName(shareName.ToString());

            if (windowsShare == null)
            {
                throw new ShareException(string.Format("Could not find share {0}.", shareName));
            }
            WindowsShare.MethodStatus methodStatus = windowsShare.SetPermission(domain, user, accessMask);
            if (methodStatus != WindowsShare.MethodStatus.Success)
            {
                throw new ShareException(string.Format("Could not set AccessMask {0} for user {1}\\{2} on share {3}", accessMask.ToString(), domain, user, shareName));
            }
            logger.LogInfo(string.Format("Share permissings set for {0}.", shareName));
        }
コード例 #4
0
 public static void CreateLocalShareDirectoryIfNotExisting(ILogger logger, DirectoryPath directoryPath, ShareName shareName, string domain, string user)
 {
     try
     {
         DirectoryHelper.CreateLocalDirectoryIfNotExistingAndGiveFullControlToUser(logger, directoryPath, domain, user);
         DirectoryHelper.TestReadWriteAccessToDirectory(logger, directoryPath);
         string shareDescription = string.Format("Shared {0} with {1} on {2}", directoryPath, shareName, DateTime.UtcNow.ToLongDateString());
         if (WindowsShare.GetShareByName(shareName.ToString()) == null)
         {
             logger.LogDebug(string.Format("No share existing. Creating share {0}.", shareName));
             ShareFolder(logger, directoryPath, shareName, shareDescription);
             SharePermissions(logger, shareName, domain, user, WindowsShare.AccessMaskTypes.FullControl);
         }
         TestReadWriteAccessToShare(logger, new UncPath(new ServerName(Environment.MachineName), shareName));
     }
     catch (Exception ex)
     {
         throw new ShareException("Error sharing folders.", ex);
     }
 }