Exemplo n.º 1
0
        private static void CheckWritePermission(string path)
        {
            WindowsIdentity  identity           = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal          = new WindowsPrincipal(identity);
            bool             isInRoleWithAccess = false;

            try
            {
                DirectoryInfo               di    = new DirectoryInfo(DirectoryUtils.ToLongPath(path));
                DirectorySecurity           ds    = di.GetAccessControl();
                AuthorizationRuleCollection rules = ds.GetAccessRules(true, true, typeof(NTAccount));

                foreach (AuthorizationRule rule in rules)
                {
                    FileSystemAccessRule fsAccessRule = rule as FileSystemAccessRule;
                    if (fsAccessRule == null)
                    {
                        continue;
                    }

                    if ((fsAccessRule.FileSystemRights & FileSystemRights.Write) != 0)
                    {
                        NTAccount ntAccount = rule.IdentityReference as NTAccount;
                        if (ntAccount == null)
                        {
                            continue;
                        }

                        if (principal.IsInRole(ntAccount.Value))
                        {
                            if (fsAccessRule.AccessControlType == AccessControlType.Deny)
                            {
                                isInRoleWithAccess = false;
                                break;
                            }
                            isInRoleWithAccess = true;
                        }
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
            }

            if (!isInRoleWithAccess)
            {
                // is run as administrator?
                if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                {
                    return;
                }

                // try run as admin
                Process  proc = new Process();
                string[] args = Environment.GetCommandLineArgs();
                proc.StartInfo.FileName        = args[0];
                proc.StartInfo.Arguments       = string.Join(" ", args.Skip(1).Select(t => $"\"{t}\""));
                proc.StartInfo.UseShellExecute = true;
                proc.StartInfo.Verb            = "runas";

                try
                {
                    proc.Start();
                    Environment.Exit(0);
                }
                catch (Win32Exception ex)
                {
                    //The operation was canceled by the user.
                    const int ERROR_CANCELLED = 1223;
                    if (ex.NativeErrorCode == ERROR_CANCELLED)
                    {
                        Logger.Instance.Log(LogType.Error, LogCategory.General, $"You can't export to folder {path} without Administrator permission");
                        Console.ReadKey();
                    }
                    else
                    {
                        Logger.Instance.Log(LogType.Error, LogCategory.General, $"You have to restart application as Administator in order to export to folder {path}");
                        Console.ReadKey();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public WebGLStructure(FileCollection collection, string rootPath) :
            base(collection)
        {
            if (string.IsNullOrEmpty(rootPath))
            {
                throw new ArgumentNullException(rootPath);
            }
            m_root = new DirectoryInfo(DirectoryUtils.ToLongPath(rootPath));
            if (!m_root.Exists)
            {
                throw new Exception($"Directory '{rootPath}' doesn't exist");
            }

            Dictionary <string, string> files = new Dictionary <string, string>();
            string buildPath = Path.Combine(m_root.FullName, BuildName);

            if (Directory.Exists(buildPath))
            {
                DirectoryInfo buildDirectory = new DirectoryInfo(buildPath);
                foreach (FileInfo file in buildDirectory.EnumerateFiles())
                {
                    if (file.Name.EndsWith(DataWebExtension, StringComparison.Ordinal))
                    {
                        Name = file.Name.Substring(0, file.Name.Length - DataWebExtension.Length);
                        files.Add(Name, file.FullName);
                        break;
                    }
                }
                DataPathes = new string[] { rootPath, buildPath };
            }
            else
            {
                string developmentPath = Path.Combine(m_root.FullName, DevelopmentName);
                if (Directory.Exists(developmentPath))
                {
                    DirectoryInfo buildDirectory = new DirectoryInfo(developmentPath);
                    foreach (FileInfo file in buildDirectory.EnumerateFiles())
                    {
                        if (file.Extension == DataExtension)
                        {
                            Name = file.Name.Substring(0, file.Name.Length - DataExtension.Length);
                            files.Add(Name, file.FullName);
                            break;
                        }
                    }
                    DataPathes = new string[] { rootPath, developmentPath };
                }
                else
                {
                    string releasePath = Path.Combine(m_root.FullName, ReleaseName);
                    if (Directory.Exists(releasePath))
                    {
                        DirectoryInfo buildDirectory = new DirectoryInfo(releasePath);
                        foreach (FileInfo file in buildDirectory.EnumerateFiles())
                        {
                            if (file.Extension == DataGzExtension)
                            {
                                Name = file.Name.Substring(0, file.Name.Length - DataGzExtension.Length);
                                files.Add(Name, file.FullName);
                                break;
                            }
                        }
                        DataPathes = new string[] { rootPath, releasePath };
                    }
                    else
                    {
                        throw new Exception("Build directory hasn't been found");
                    }
                }
            }

            if (files.Count == 0)
            {
                throw new Exception("No files has been found");
            }

            CollectStreamingAssets(m_root, files);
            Files = files;

            Assemblies = new Dictionary <string, string>();
            //m_fileCollection.AssemblyManager.ScriptingBackEnd = ScriptingBackEnd.Il2Cpp;
        }