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));
        }
 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);
     }
 }