示例#1
0
        public static void SetManifest(string exe, string manifestName)
        {
            Mutex x = new Mutex(false, MutexName);

            x.WaitOne();

            try
            {
                // Manifest file name
                string filename = Path.Combine(Paths.ManifestsDir, manifestName);
                if (File.Exists(filename) == false)
                {
                    throw new FileNotFoundException(filename);
                }

                FileInfo fi = new FileInfo(exe);

                // Copy exe file to a temporary directory
                string exeTmp = IO.CreateTempFileNameByExt(".exe");
                IO.FileCopy(exe, exeTmp);

                string mtFileName = Path.Combine(Paths.MicrosoftSDKBinDir, "mt.exe");
                string mtArgs     = string.Format("-nologo -manifest \"{0}\" -outputresource:\"{1}\";1", filename, exeTmp);

                Exception ex = null;

                int i;
                // Repeated 20 times in order to avoid locking the file by the anti-virus software
                for (i = 0; i < 20; i++)
                {
                    try
                    {
                        // Execute
                        Win32BuildTool.ExecCommand(mtFileName, mtArgs, false, true);
                        ex = null;

                        break;
                    }
                    catch (Exception ex2)
                    {
                        ex = ex2;
                    }

                    ThreadObj.Sleep(Secure.Rand31i() % 50);
                }

                if (ex != null)
                {
                    throw new ApplicationException("mt.exe Manifest Processing for '" + exe + "' Failed.");
                }

                ex = null;

                // Revert to the original file
                for (i = 0; i < 20; i++)
                {
                    try
                    {
                        IO.FileCopy(exeTmp, exe);
                        ex = null;

                        break;
                    }
                    catch (Exception ex2)
                    {
                        ex = ex2;
                    }

                    ThreadObj.Sleep(Secure.Rand31i() % 50);
                }

                // Restore the date and time
                File.SetCreationTime(exe, fi.CreationTime);
                File.SetLastAccessTime(exe, fi.LastAccessTime);
                File.SetLastWriteTime(exe, fi.LastWriteTime);
            }
            finally
            {
                x.ReleaseMutex();
            }
        }
示例#2
0
 public static void Test()
 {
     Win32BuildTool.ExecCommand(@"C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86\mt.exe",
                                "-test", no_stdout: true);
 }