コード例 #1
0
        public static bool TryCreateDirectory(string serverFQDN, string directoryPath, FileSystemAccessRule[] directoryPermissions, Task.TaskVerboseLoggingDelegate verboseDelegate, Task.TaskWarningLoggingDelegate warningDelegate)
        {
            bool flag = false;

            if (string.Compare(serverFQDN, NativeHelpers.GetLocalComputerFqdn(false), StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                try
                {
                    verboseDelegate(Strings.VerboseCheckDirectoryExistenceCondition(serverFQDN, directoryPath));
                    if (Directory.Exists(directoryPath))
                    {
                        if (directoryPermissions != null)
                        {
                            DirectorySecurity directorySecurity = SystemConfigurationTasksHelper.CreateDirectorySecurityHelper(directoryPermissions);
                            directorySecurity.SetAccessRuleProtection(true, false);
                            verboseDelegate(Strings.VerboseSetAccessControl(serverFQDN, directoryPath));
                            TaskLogger.Trace("Directory \"" + directoryPath + "\" already exists on the local machine, just set the access control.", new object[0]);
                            Directory.SetAccessControl(directoryPath, directorySecurity);
                            flag = true;
                        }
                        else
                        {
                            verboseDelegate(Strings.VerboseNoAccessControlSpecified(serverFQDN, directoryPath));
                            TaskLogger.Trace("No access control specified to be set on directory \"" + directoryPath + "\".", new object[0]);
                            flag = true;
                        }
                    }
                    else if (directoryPermissions != null)
                    {
                        DirectorySecurity directorySecurity = SystemConfigurationTasksHelper.CreateDirectorySecurityHelper(directoryPermissions);
                        directorySecurity.SetAccessRuleProtection(true, false);
                        verboseDelegate(Strings.VerboseCreateDirectory(serverFQDN, directoryPath));
                        TaskLogger.Trace("Directory \"" + directoryPath + "\" does not exist.  Create the directory on the local machine and set the access control.", new object[0]);
                        Directory.CreateDirectory(directoryPath, directorySecurity);
                        flag = true;
                    }
                    else
                    {
                        verboseDelegate(Strings.VerboseCreateDirectory(serverFQDN, directoryPath));
                        verboseDelegate(Strings.VerboseNoAccessControlSpecified(serverFQDN, directoryPath));
                        TaskLogger.Trace("Directory \"" + directoryPath + "\" does not exist.  Create the directory on the local machine.  No access controls specified to set on the directory.", new object[0]);
                        Directory.CreateDirectory(directoryPath);
                        flag = true;
                    }
                    goto IL_43C;
                }
                catch (NotSupportedException ex)
                {
                    verboseDelegate(Strings.VerboseFailedCreateDirectory(serverFQDN, directoryPath, ex.Message));
                    TaskLogger.Trace("this.TryCreateDirectory() raised exception: {0}", new object[]
                    {
                        ex.ToString()
                    });
                    goto IL_43C;
                }
                catch (ArgumentNullException ex2)
                {
                    verboseDelegate(Strings.VerboseFailedCreateDirectory(serverFQDN, directoryPath, ex2.Message));
                    TaskLogger.Trace("this.TryCreateDirectory() raised exception: {0}", new object[]
                    {
                        ex2.ToString()
                    });
                    goto IL_43C;
                }
                catch (UnauthorizedAccessException ex3)
                {
                    verboseDelegate(Strings.VerboseFailedCreateDirectory(serverFQDN, directoryPath, ex3.Message));
                    TaskLogger.Trace("this.TryCreateDirectory() raised exception: {0}", new object[]
                    {
                        ex3.ToString()
                    });
                    goto IL_43C;
                }
                catch (ArgumentException ex4)
                {
                    verboseDelegate(Strings.VerboseFailedCreateDirectory(serverFQDN, directoryPath, ex4.Message));
                    TaskLogger.Trace("this.TryCreateDirectory() raised exception: {0}", new object[]
                    {
                        ex4.ToString()
                    });
                    goto IL_43C;
                }
                catch (IOException ex5)
                {
                    verboseDelegate(Strings.VerboseFailedCreateDirectory(serverFQDN, directoryPath, ex5.Message));
                    TaskLogger.Trace("this.TryCreateDirectory() raised exception: {0}", new object[]
                    {
                        ex5.ToString()
                    });
                    goto IL_43C;
                }
            }
            try
            {
                verboseDelegate(Strings.VerboseCheckDirectoryExistenceCondition(serverFQDN, directoryPath));
                uint num;
                if (!WmiWrapper.IsDirectoryExisting(serverFQDN, directoryPath))
                {
                    verboseDelegate(Strings.VerboseCreateDirectory(serverFQDN, directoryPath));
                    TaskLogger.Trace(string.Concat(new string[]
                    {
                        "Create the directory \"",
                        directoryPath,
                        "\" on the remote machine \"",
                        serverFQDN,
                        "\"."
                    }), new object[0]);
                    num = WmiWrapper.CreateDirectory(serverFQDN, directoryPath);
                }
                else
                {
                    num = 0U;
                }
                if (num == 0U)
                {
                    if (directoryPermissions != null)
                    {
                        verboseDelegate(Strings.VerboseSetAccessControl(serverFQDN, directoryPath));
                        TaskLogger.Trace(string.Concat(new string[]
                        {
                            "Set the access control for directory \"",
                            directoryPath,
                            "\" on the remote machine \"",
                            serverFQDN,
                            "\"."
                        }), new object[0]);
                        num = WmiWrapper.ChangeSecurityPermissions(serverFQDN, directoryPath, directoryPermissions);
                        if (num == 0U)
                        {
                            flag = true;
                        }
                        else
                        {
                            verboseDelegate(Strings.VerboseFailedSetAccessControl(serverFQDN, directoryPath, "WMI Status: " + num.ToString()));
                            TaskLogger.Trace("this.TryCreateDirectory() failed to set the access control on the directory - WMI Status: {0}", new object[]
                            {
                                num.ToString()
                            });
                        }
                    }
                    else
                    {
                        verboseDelegate(Strings.VerboseNoAccessControlSpecified(serverFQDN, directoryPath));
                        TaskLogger.Trace("No access control specified to be set on directory \"" + directoryPath + "\".", new object[0]);
                        flag = true;
                    }
                }
                else
                {
                    verboseDelegate(Strings.VerboseFailedCreateDirectory(serverFQDN, directoryPath, "WMI Status: " + num.ToString()));
                    TaskLogger.Trace("this.TryCreateDirectory() failed to create the directory - WMI Status: {0}", new object[]
                    {
                        num.ToString()
                    });
                }
            }
            catch (WmiException ex6)
            {
                verboseDelegate(Strings.VerboseFailedCreateDirectory(serverFQDN, directoryPath, ex6.Message));
                TaskLogger.Trace("this.TryCreateDirectory() raised exception: {0}", new object[]
                {
                    ex6.ToString()
                });
            }
            catch (UnauthorizedAccessException ex7)
            {
                verboseDelegate(Strings.ErrorFailedToConnectToServer(serverFQDN, ex7.Message));
                TaskLogger.Trace("this.TryCreateDirectory() raised exception: {0}", new object[]
                {
                    ex7.ToString()
                });
            }
IL_43C:
            if (!flag)
            {
                warningDelegate(Strings.WarningFailedCreateDirectory(serverFQDN, directoryPath));
            }
            return(flag);
        }