I've finished an application in C# using Visual Studio 2022 and need to make an installer for it. I used Microsoft Visual Studio Installer Projects extension and could create the installer which works fine. But I need something else: during instalation, it's necessary to create a folder, then a file and save this file into that folder. But I don't know how to do this in this installer. Does anybody has a solution for this? Thanks.
Loading

Rajanikant HawaldarRank #110 · 20.2K reputationPosted Sep 12, 2022, 11:41 PM EDT
if want to do something during installation then go to setup solution right click add new item => Installer file then override methods(Committed, Install, Commit, Rollback)
Ismael Oliveira163 reputationPosted Sep 13, 2022, 9:55 AM EDT
Rajanikant.
Thanks for your answer. But I didn't understand what you mean by ==> installer file, then override methods. Could you detail this idea for me? Thank you again.
Amit MohantyRank #112 · 20K reputationPosted Sep 15, 2022, 6:51 AM EDT
Hey check the below link, this may help you
https://www.c-sharpcorner.com/UploadFile/27c648/create-a-custom-setup-for-change-app-onfig/
Rijwan AnsariRank #82 · 24.2K reputationPosted Sep 17, 2022, 1:19 AM EDT
Alternatively, you can write code in main form load to run first time and create a folder and a file.
Something like below:
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string filename = "counter.txt"; if (!System.IO.File.Exists(filename)) { System.IO.File.WriteAllText(filename, "1"); Application.Run(new Form1()); } else { Application.Run(new Form2()); } }Rajanikant HawaldarRank #110 · 20.2K reputationPosted Sep 17, 2022, 7:18 AM EDT
Check below article, they starting service after install setup and stopping service after uninstall setup, similerly you can do do override methos like Install, etc as below
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Linq; using System.Threading.Tasks; namespace WinformTest { [RunInstaller(true)] public partial class Installer : System.Configuration.Install.Installer { public Installer() { InitializeComponent(); } public override void Install(IDictionary stateSaver) { // Do whatever you want base.Install(stateSaver); } protected override void OnAfterInstall(IDictionary savedState) { // Do whatever you want base.OnAfterInstall(savedState); } public override void Commit(IDictionary savedState) { // Do whatever you want base.Commit(savedState); } } }The installer.cs class has many method to override you can use as per your need
#region Assembly System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Configuration.Install.dll #endregion using System.Collections; using System.ComponentModel; using System.Configuration.Install; namespace System.Configuration.Install { // // Summary: // Provides the foundation for custom installations. [DefaultEvent("AfterInstall")] public class Installer : Component { // // Summary: // Initializes a new instance of the System.Configuration.Install.Installer class. public Installer(); // // Summary: // Gets or sets the installer containing the collection that this installer belongs // to. // // Returns: // An System.Configuration.Install.Installer containing the collection that this // instance belongs to, or null if this instance does not belong to a collection. [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [ResDescriptionAttribute("Desc_Installer_Parent")] [TypeConverter(typeof(InstallerParentConverter))] public Installer Parent { get; set; } // // Summary: // Gets the collection of installers that this installer contains. // // Returns: // An System.Configuration.Install.InstallerCollection containing the collection // of installers associated with this installer. [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public InstallerCollection Installers { get; } // // Summary: // Gets the help text for all the installers in the installer collection. // // Returns: // The help text for all the installers in the installer collection, including the // description of what the installer does and the command line options for the installation // executable, such as the InstallUtil.exe utility, that can be passed to and understood // by this installer. // // Exceptions: // T:System.NullReferenceException: // One of the installers in the installer collection specifies a null reference // instead of help text. A likely cause for this exception is that a field to contain // the help text is defined but not initialized. [ResDescriptionAttribute("Desc_Installer_HelpText")] public virtual string HelpText { get; } // // Summary: // Gets or sets information about the current installation. // // Returns: // An System.Configuration.Install.InstallContext that contains information about // the current installation. [Browsable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public InstallContext Context { get; set; } // // Summary: // Occurs before the System.Configuration.Install.Installer.Install(System.Collections.IDictionary) // method of each installer in the installer collection has run. public event InstallEventHandler BeforeInstall; // // Summary: // Occurs before the installers in the System.Configuration.Install.Installer.Installers // property committ their installations. public event InstallEventHandler Committing; // // Summary: // Occurs after all the installers in the System.Configuration.Install.Installer.Installers // property perform their uninstallation operations. public event InstallEventHandler AfterUninstall; // // Summary: // Occurs after the installations of all the installers in the System.Configuration.Install.Installer.Installers // property are rolled back. public event InstallEventHandler AfterRollback; // // Summary: // Occurs after the System.Configuration.Install.Installer.Install(System.Collections.IDictionary) // methods of all the installers in the System.Configuration.Install.Installer.Installers // property have run. public event InstallEventHandler AfterInstall; // // Summary: // Occurs after all the installers in the System.Configuration.Install.Installer.Installers // property have committed their installations. public event InstallEventHandler Committed; // // Summary: // Occurs before the installers in the System.Configuration.Install.Installer.Installers // property are rolled back. public event InstallEventHandler BeforeRollback; // // Summary: // Occurs before the installers in the System.Configuration.Install.Installer.Installers // property perform their uninstall operations. public event InstallEventHandler BeforeUninstall; // // Summary: // When overridden in a derived class, completes the install transaction. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer after // all the installers in the collection have run. // // Exceptions: // T:System.ArgumentException: // The savedState parameter is null.-or- The saved-state System.Collections.IDictionary // might have been corrupted. // // T:System.Configuration.Install.InstallException: // An exception occurred during the System.Configuration.Install.Installer.Commit(System.Collections.IDictionary) // phase of the installation. This exception is ignored and the installation continues. // However, the application might not function correctly after the installation // is complete. public virtual void Commit(IDictionary savedState); // // Summary: // When overridden in a derived class, performs the installation. // // Parameters: // stateSaver: // An System.Collections.IDictionary used to save information needed to perform // a commit, rollback, or uninstall operation. // // Exceptions: // T:System.ArgumentException: // The stateSaver parameter is null. // // T:System.Exception: // An exception occurred in the System.Configuration.Install.Installer.BeforeInstall // event handler of one of the installers in the collection.-or- An exception occurred // in the System.Configuration.Install.Installer.AfterInstall event handler of one // of the installers in the collection. public virtual void Install(IDictionary stateSaver); // // Summary: // When overridden in a derived class, restores the pre-installation state of the // computer. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the pre-installation state of // the computer. // // Exceptions: // T:System.ArgumentException: // The savedState parameter is null.-or- The saved-state System.Collections.IDictionary // might have been corrupted. // // T:System.Configuration.Install.InstallException: // An exception occurred during the System.Configuration.Install.Installer.Rollback(System.Collections.IDictionary) // phase of the installation. This exception is ignored and the rollback continues. // However, the computer might not be fully reverted to its initial state after // the rollback completes. public virtual void Rollback(IDictionary savedState); // // Summary: // When overridden in a derived class, removes an installation. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer after // the installation was complete. // // Exceptions: // T:System.ArgumentException: // The saved-state System.Collections.IDictionary might have been corrupted. // // T:System.Configuration.Install.InstallException: // An exception occurred while uninstalling. This exception is ignored and the uninstall // continues. However, the application might not be fully uninstalled after the // uninstallation completes. public virtual void Uninstall(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.AfterInstall event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer after // all the installers contained in the System.Configuration.Install.Installer.Installers // property have completed their installations. protected virtual void OnAfterInstall(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.AfterRollback event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer after // the installers contained in the System.Configuration.Install.Installer.Installers // property are rolled back. protected virtual void OnAfterRollback(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.AfterUninstall event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer after // all the installers contained in the System.Configuration.Install.Installer.Installers // property are uninstalled. protected virtual void OnAfterUninstall(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.BeforeInstall event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer before // the installers in the System.Configuration.Install.Installer.Installers property // are installed. This System.Collections.IDictionary object should be empty at // this point. protected virtual void OnBeforeInstall(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.BeforeRollback event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer before // the installers in the System.Configuration.Install.Installer.Installers property // are rolled back. protected virtual void OnBeforeRollback(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.BeforeUninstall event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer before // the installers in the System.Configuration.Install.Installer.Installers property // uninstall their installations. protected virtual void OnBeforeUninstall(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.Committed event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer after // all the installers in the System.Configuration.Install.Installer.Installers property // run. protected virtual void OnCommitted(IDictionary savedState); // // Summary: // Raises the System.Configuration.Install.Installer.Committing event. // // Parameters: // savedState: // An System.Collections.IDictionary that contains the state of the computer before // the installers in the System.Configuration.Install.Installer.Installers property // are committed. protected virtual void OnCommitting(IDictionary savedState); } }https://www.c-sharpcorner.com/article/how-to-perform-custom-actions-and-upgrade-using-visual-studio-installer/
Ismael Oliveira163 reputationPosted Sep 19, 2022, 6:38 PM EDT
Rajanikant.
Your suggestion seams to be OK, but when I try to add new item, then Installer class (which adds the line:
using System.Configuration.Install;), I receive the message "The reference 'System.Configuration.Install' couldn't be added to your project". Do you have an idea of what it means? Is it a matter of versions?
Sorry to disturb you, but the answer is almost here. Thank you again.
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Sep 19, 2022, 10:18 PM EDT
Add that dll from nuget