コード例 #1
0
        public void UninstallOffice(string installVer = "2016")
        {
            const string configurationXml = "<Configuration><Remove All=\"TRUE\"/><Display Level=\"Full\" /></Configuration>";

            var tmpPath         = Environment.ExpandEnvironmentVariables("%temp%");
            var embededExeFiles = EmbeddedResources.GetEmbeddedItems(tmpPath, @"\.exe$");

            //NOTE: Have this function determine if 2013 ProPlus or 2016 ProPlus is installed and then use the right ODT version
            var installExe = tmpPath + @"\" + embededExeFiles.FirstOrDefault(f => f.ToLower().Contains("2016"));

            if (installVer == "2013")
            {
                //If 2013 then get the 2013 ODT version
                installExe = tmpPath + @"\" + embededExeFiles.FirstOrDefault(f => f.ToLower().Contains("2013"));
            }
            var xmlPath = tmpPath + @"\configuration.xml";

            if (System.IO.File.Exists(xmlPath))
            {
                System.IO.File.Delete(xmlPath);
            }
            System.IO.File.WriteAllText(xmlPath, configurationXml);

            var p = new Process
            {
                StartInfo = new ProcessStartInfo()
                {
                    FileName               = installExe,
                    Arguments              = "/configure " + tmpPath + @"\configuration.xml",
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true
                },
            };

            p.Start();
            p.WaitForExit();

            var error = p.StandardError.ReadToEnd();

            if (!string.IsNullOrEmpty(error))
            {
                throw (new Exception(error));
            }

            if (System.IO.File.Exists(xmlPath))
            {
                System.IO.File.Delete(xmlPath);
            }

            foreach (var exeFilePath in embededExeFiles)
            {
                try
                {
                    if (System.IO.File.Exists(tmpPath + @"\" + exeFilePath))
                    {
                        System.IO.File.Delete(tmpPath + @"\" + exeFilePath);
                    }
                }
                catch { }
            }
        }