简体   繁体   English

使用c#安装Msi文件

[英]Install Msi file using c#

I am trying to insert msi file using asp.net application.我正在尝试使用 asp.net 应用程序插入 msi 文件。 when i run visual studio in administrators mode it is working fine but when i run it in normal mode it is not working.当我在管理员模式下运行 Visual Studio 时它工作正常但是当我在正常模式下运行它时它不起作用。 I had tried following code:我曾尝试以下代码:

string installerFilePath;
installerFilePath = @"D:\ActivexPractice\test\test\NewFolder1\setup.msi";
System.Diagnostics.Process installerProcess = System.Diagnostics.Process.Start(installerFilePath, "/q");

can any body guide me on this how to install it without administrators right任何机构都可以指导我如何在没有管理员的情况下安装它

You can use msiexec.exe to run installer.您可以使用 msiexec.exe 来运行安装程序。 Here is sample code.这是示例代码。

        Process installerProcess = new Process();
        ProcessStartInfo processInfo = new ProcessStartInfo();
        processInfo.Arguments = @"/i  D:\ActivexPractice\test\test\NewFolder1\setup.msi  /q";
        processInfo.FileName = "msiexec";
        installerProcess.StartInfo = processInfo;
        installerProcess.Start();
        installerProcess.WaitForExit();

If the MSI requires admin rights to install then it will ask for elevation in a UI install.如果 MSI 需要管理员权限才能安装,那么它会在 UI 安装中要求提升。 Your /q is failing because a silent install is really silent and will not prompt for elevation.您的 /q 失败,因为静默安装确实是静默的,并且不会提示提升。 Note that limited users are not allowed to break security rules simply because they are doing an install.请注意,不允许受限用户仅仅因为进行安装而违反安全规则。

So in that situation your launching process would need to be elevated, either by running as administrator or giving it a requiresAdministrator manifest so it asks for elevation.因此,在这种情况下,您的启动过程需要提升,以管理员身份运行或为其提供 requiresAdministrator 清单,以便它要求提升。

When you fire off the install you need to make sure that your elevated state is used to fire off the install.当您启动安装时,您需要确保您的提升状态用于启动安装。 The simplest way to guarantee this is to just call (p/invoke to...) MsiInstallProduct () directly from your code.保证这一点的最简单方法是直接从您的代码中调用 (p/invoke to...) MsiInstallProduct ()。 The issue with Process.Start is that by default ProcessStartInfo.UseShellExecute is true and your elevated state (if you have one) will not be used to start the install. Process.Start 的问题在于,默认情况下 ProcessStartInfo.UseShellExecute 为 true,并且您的提升状态(如果有)将不会用于启动安装。 When the install is launched it needs to be a CreateProcess type of execution rather than a ShellExecute type so that your elevated credentials are used.当安装启动时,它需要是 CreateProcess 类型的执行而不是 ShellExecute 类型,以便使用您提升的凭据。

private void installMSI(string path) { string[] allFiles = Directory.GetFiles(path, "*.msi"); private void installMSI(string path) { string[] allFiles = Directory.GetFiles(path, "*.msi");

        foreach (string file in allFiles)
        {
            if(file.Contains("Artis"))
            {
                System.Diagnostics.Process installerProcess = 
                System.Diagnostics.Process.Start(file, "/q");
                while (installerProcess.HasExited == false)
                {
                    installerProcess.WaitForExit();
                }
                Console.WriteLine("Installation done for" + file);
            }
            
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM