21

Is it possible to use NGen with ClickOnce deployment?

GEOCHET
  • 20,623
  • 15
  • 71
  • 98
Branko
  • 525
  • 6
  • 15

2 Answers2

17

Actually you can use NGEN and clickone, but you are going to need to run the NGEN after the clickonce installation has happened, since NGEN is part of the .NET installation (for 3.5 you should refer to the 2.0 installation).

Here is an example, I think it is generic enough for you to use it without changing or doing very little changes to the code (except for the call to your form):

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        if (ApplicationDeployment.IsNetworkDeployed && ApplicationDeployment.CurrentDeployment.IsFirstRun)
        {

            string appPath = Application.StartupPath;
            string winPath = Environment.GetEnvironmentVariable("WINDIR");

            Process proc = new Process();
            System.IO.Directory.SetCurrentDirectory(appPath);

            proc.EnableRaisingEvents = false;
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            proc.StartInfo.FileName = winPath + @"\Microsoft.NET\Framework\v2.0.50727\ngen.exe";
            proc.StartInfo.Arguments = "uninstall " + Application.ProductName + " /nologo /silent";

            proc.Start();
            proc.WaitForExit();

            proc.StartInfo.FileName = winPath + @"\Microsoft.NET\Framework\v2.0.50727\ngen.exe";
            proc.StartInfo.Arguments = "install " + Application.ProductName + " /nologo /silent";

            proc.Start();
            proc.WaitForExit();

        }

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294
Ron
  • 202
  • 2
  • 3
  • 5
    This does not work on Windows 7 (and I guess Vista), because `ngen` wants to run as Administrator. – Evgeniy Berezovsky Apr 11 '12 at 01:37
  • 3
    This is not much practical use as it requires admin permissions. The nature of clickonce is that it doesn't require admin permissions to install, so adding this code wouldn't make sense. – pmcilreavy Jul 15 '14 at 21:38
4

No, you can not. See http://social.msdn.microsoft.com/Forums/en-US/clr/thread/a41b62c5-bdee-4bd5-9811-15a35c4a4add/. You need to create a regular installer file for that.

liggett78
  • 11,058
  • 2
  • 27
  • 27