Makinde A. Israel

Makinde A. Israel

  • NA
  • 166
  • 19.4k

Directory not created during Custom Action, VS2010 Installer

Mar 27 2017 10:44 AM
Good day sirs/ma's
 
Please am trying to write a custom action library for vs 2010 installer. In the custom action's Install event, am trying to copy some files (among which is an sqlite database) to locations like the "AppData\Local\MySoftware" and some in the "My Document" folder. I know I should have used the "File System on Target Machine" property of the VS2010 Installer package and add those folders but I need to iterate through some list of files and add them to the "My Document" and the "MySoftware" folder plus I want to store the directory of each files into the registry.
 
I created a new class that inherited from the Installer Class and override the Install event, then put the bellow codes but none of the folders were created, not a single value was writen to the registry and no error was throw, the package installed successfully but those directories, folders and registry path were not created.
 
I tried calling the Install library(the custom action libraray I wrote) from the main form's Load event, it worked and all registry and folders were created, suggesting that nothing is wrong with my codes.
 
please help me.
 
 public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            
            try
            {
                string workingdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Application.CompanyName, Application.ProductName);
                string datapath = Path.Combine(workingdir, "Data");
                string logopath = Path.Combine(datapath, "logo01.png");
                string bannerpath = Path.Combine(datapath, "banner01.jpg");
                string bgpath = Path.Combine(datapath, "defualt1.jpg");
                string backupdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Application.CompanyName, Application.ProductName);
                string dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Application.CompanyName, Application.ProductName);
                string dbdir = Path.Combine(dbpath, "ItemBank.db");
                string userpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Application.CompanyName, Application.ProductName, "userdata");
                string exampath = Path.Combine(datapath, "Exams");
                string configpath = Path.Combine(datapath, "config.gzsettings");

if (!Directory.Exists(exampath))
                    Directory.CreateDirectory(exampath);
                if (!Directory.Exists(userpath))
                    Directory.CreateDirectory(userpath);
                if (!Directory.Exists(dbpath))
                    Directory.CreateDirectory(dbpath);
                if (!Directory.Exists(backupdir))
                    Directory.CreateDirectory(backupdir);

Logger loger = new Logger(workingdir);

if (File.Exists(@"Data\ItemBank.db"))
                {
                    if (File.Exists(dbdir))
                    {
                        string file = Path.Combine(dbpath, "ItemBank_old.db");
                        if (File.Exists(file))
                        {
                            int i = 1;
                            do
                            {
                                file = string.Format("{0}\\ItemBank_old {1}.db", dbpath, i);
                                i++;
                            }
                            while (File.Exists(file));
                        }

File.Copy(dbdir, file);
                    }

File.Copy(@"Data\ItemBank.db", dbdir, true);
                    File.Copy(@"Data\ItemBank.db", backupdir + @"\ItemBank.db", true);
                }

                if (File.Exists(@"config.gzsettings"))
                {
                    if (File.Exists(configpath))
                    {
                        string file = Path.Combine(datapath, "config_old.gzsettings");
                        if (File.Exists(file))
                        {
                            int i = 1;
                            do
                            {
                                file = string.Format("{0}\\config_old {1}.gzsettings", datapath, i);
                                i++;
                            }
                            while (File.Exists(file));
                        }

File.Copy(configpath, file);
                    }
                    File.Copy(@"config.gzsettings", configpath, true);
                    File.Copy(@"config.gzsettings", backupdir + @"\config.gzsettings", true);
                }

if (Directory.Exists(@"Data\Exams"))
                {
                    foreach (string file in Directory.GetFiles(@"Data\Exams"))
                    {
                        int s = file.LastIndexOf("\\");
                        string pat = Path.Combine(exampath, file.Substring(s));
                        File.Copy(file, Path.Combine(backupdir, file.Substring(s)), true);
                    }
                }

if (File.Exists(@"Data\default1.jpg"))
                {
                    File.Copy(@"Data\default1.jpg", bgpath, true);
                    File.Copy(@"Data\default1.jpg", backupdir + @"\default1.jpg", true);
                }

if (File.Exists(@"Data\logo01.jpg"))
                {
                    File.Copy(@"Data\logo01.jpg", logopath, true);
                    File.Copy(@"Data\logo01.jpg", backupdir + @"\logo01.jpg", true);
                }

RegistryKey rgk_paths = Application.UserAppDataRegistry.CreateSubKey("UsefulPaths");
                rgk_paths.SetValue("usersettingspath", configpath);
                rgk_paths.SetValue("itembankpath", dbdir);
                rgk_paths.SetValue("appdatapath", datapath);
                rgk_paths.SetValue("userdatapath", userpath);
                rgk_paths.SetValue("backuppath", backupdir);

                if (File.Exists(@"config.gzsettings"))
                {
                    Settings settings = new Settings();
                    settings.Load(@"config.gzsettings");
                    settings["userinfofolderpath"] = userpath;
                    settings["appdatapath"] = datapath;
                    settings["ItemBankPath"] = dbdir;
                    settings["examfolderpath"] = exampath;
                    settings["LogoPath"] = logopath;
                    settings["BackgroundImagePath"] = bgpath;
                    settings["BarnerPath"] = bannerpath;

using (RegistryKey rgk_settings = Application.UserAppDataRegistry.CreateSubKey("defaultsettings"))
                    {
                        foreach (var key in settings.GetData().Keys)
                        {
                            rgk_settings.SetValue(key, settings[key]);
                        }
                        rgk_settings.Close();
                    }

settings.Save(backupdir + @"\config.gzsettings");
                    settings.Save(configpath);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException("Database creation fault: \n" + e.Message);
            }
        }