Silent Installation Of Applications Using C#

There will be situations where you will need to install applications silently without any human intervention. This is a pretty normal use case for System Administrators and they love to automate their tasks. In fact knowledge of this subject may be handy for any technology worker in various circumstances. In this article we will silently install FileZilla on to the computer. Before we proceed further, here are a few things to keep in mind:

  • To install an application silently using the installer exe/package, you must use the Silent Switch for silent installation as described by the Vendor of the Software. For example Silent Switch for FileZilla is /S. It may vary for different applications.

  • Whether the application will install silently depends on how the packaging of the installer exe was done by the Vendor. So keep in mind that not every application can be installed silently.

  • You can use PowerShell cmdlets/ PowerShell Scripts/ Batch Scripts from within your C# application to achieve Silent Installation of applications. In this tutorials we will be using PowerShell cmdlet to achieve the same.

Scenario: In my case here I have the installer exes in "ApplicationRepository" folder in C drive as shown below.

ApplicationRepository

We will only be installing FileZilla as of now but you can extend it for any number of applications as you wish.

Also let us make sure that FileZilla is not installed by looking at installed applications in Control Panel. As we can see below, FileZilla is not currently installed.

FileZilla

Now let us see the code which does the magic i.e. installs application silently.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections.ObjectModel;  
  4. using System.IO;  
  5. using System.Linq;  
  6. using System.Text;  
  7. using System.Threading.Tasks;  
  8. using System.Management.Automation;  
  9.   
  10. namespace SilentInstallation  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.       {  
  16.             ProcessFolder();  
  17.         }  
  18.   
  19.         private static void ProcessFolder()   
  20.       {  
  21.             const string SOURCEFOLDERPATH = @ "C:\ApplicationRepository";  
  22.   
  23.             if (Directory.Exists(SOURCEFOLDERPATH)) {  
  24.                 Console.WriteLine("Directory exists at: {0}", SOURCEFOLDERPATH);  
  25.                 if (Directory.GetFiles(SOURCEFOLDERPATH, "*.exe").Length > 0) {  
  26.                     int count = Directory.GetFiles(SOURCEFOLDERPATH, "*.exe").Length;  
  27.                     string[] files = Directory.GetFiles(SOURCEFOLDERPATH, "*.exe");  
  28.   
  29.                     foreach(var file in files) {  
  30.                         var fileName = System.IO.Path.GetFileName(file);  
  31.                         var fileNameWithPath = SOURCEFOLDERPATH + "\\" + fileName;  
  32.                         Console.WriteLine("File Name: {0}", fileName);  
  33.                         Console.WriteLine("File name with path : {0}", fileNameWithPath);  
  34.                         //Deploy application  
  35.                         Console.WriteLine("Wanna install {0} application on this VM?   
  36.                             Press any key to contiune.  
  37.                             ", fileName);  
  38.                             Console.ReadKey(); DeployApplications(fileNameWithPath); Console.ReadLine();  
  39.                         }  
  40.                     }  
  41.   
  42.                 } else  
  43.                     Console.WriteLine("Directory does not exist at: {0}", SOURCEFOLDERPATH);  
  44.   
  45.             }  
  46.   
  47.   
  48.             public static void DeployApplications(string executableFilePath)  
  49.       {  
  50.                 PowerShell powerShell = null;  
  51.                 Console.WriteLine(" ");  
  52.                 Console.WriteLine("Deploying application...");  
  53.                 try {  
  54.                     using(powerShell = PowerShell.Create())  
  55.                     {  
  56.                         //here “executableFilePath” need to use in place of “  
  57.                         //'C:\\ApplicationRepository\\FileZilla_3.14.1_win64-setup.exe'”  
  58.                         //but I am using the path directly in the script.  
  59.                         powerShell.AddScript("$setup=Start-Process 'C:\\ApplicationRepository\\  
  60.                             FileZilla_3 .14 .1 _win64 - setup.exe ' -ArgumentList ' / S ' -Wait -PassThru");  
  61.   
  62.                             Collection < PSObject > PSOutput = powerShell.Invoke(); foreach(PSObject outputItem in PSOutput) {  
  63.   
  64.                                 if (outputItem != null)  
  65.                                 {  
  66.   
  67.                                     Console.WriteLine(outputItem.BaseObject.GetType().FullName);  
  68.                                     Console.WriteLine(outputItem.BaseObject.ToString() + "\n");  
  69.                                 }  
  70.                             }  
  71.   
  72.                             if (powerShell.Streams.Error.Count > 0)  
  73.                             {  
  74.                                 string temp = powerShell.Streams.Error.First().ToString();  
  75.                                 Console.WriteLine("Error: {0}", temp);  
  76.   
  77.                             } else  
  78.                                 Console.WriteLine("Installation has completed successfully.");  
  79.   
  80.                         }  
  81.                     }  
  82.                     catch (Exception ex)  
  83.                     {  
  84.                         Console.WriteLine("Error occured: {0}", ex.InnerException);  
  85.                         //throw;  
  86.                     } finally  
  87.                     {  
  88.                         if (powerShell != null)  
  89.                             powerShell.Dispose();  
  90.                     }  
  91.   
  92.                 }  
  93.             }  
  94.         }  

Now let us see this code in action using the following screenshots:



Deploying/ Installing Application



Application

Depending upon the User Control Settings, there may be a prompt to allow our App to install another app(i.e. FileZilla). If you don't want this pop up and want absolute silent installation, change your settings to Never Notify as shown below and again run the console App if required after changing the setting. (Please Note: This setting is not recommended and it's good to revert after the demo).

uac

If everything goes well, the application will be installed successfully as shown below.

application

Now again go to the Control Panel and refresh the list of installed applications. Now you will be able to see FileZilla installed on your machine.

FileZilla


In the next version I will try to write a WCF service that can run on a Windows Server and you can hit it with a POST request with relevant information to install an application when required. This concept can be used at many places especially in Server Maintenance and Application Installation.

You can download the above source code and screenshots by downloading the attached zip file.

Hope you enjoyed the article.

Thank You.

Read more articles on C# Programming: