Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write today's date to registry during installation

Status
Not open for further replies.

PRMiller2

Technical User
Jul 30, 2010
123
I'm using Visual Studio 2010's Installer to deploy a Windows Form application. I have figured out how to add keys to the registry during the installation and how to change a string value. However, I'm not sure how to set today's date.

In my case, I have created a registry key during installation at HKLM\Software\[Manufacturer] called "InstallDate." How can I have it write today's date at the time of installation?
 
DateTime.Now.ToString()

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks Jason. Unfortunately, that's not quite what I'm looking for. While in View > Editor > Registry, I'm viewing the Properties of the InstallDate key I've created. The Properties window shows the fields "(Name), Condition, Transitive, Value, and ValueType." Anything entered in Value just outputs as a string. In order to use a Now function, do I need to do so in code?
 
I cannot for the life of me figure this out. I created the following Installer1 class in my project:

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace ComplianceManager
{
    [RunInstaller(true)]
    public partial class RegistryCustomize : Installer
    {        
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            RegistrationServices regSrv = new RegistrationServices();
            regSrv.RegisterAssembly(base.GetType().Assembly,
              AssemblyRegistrationFlags.SetCodeBase);
            RegistryKey regKey = Registry.CurrentUser;
            regKey.OpenSubKey("Software\\Vianney Software\\Compliance Manager");
            regKey.SetValue("InstallDate", DateTime.Now.ToString());
        }
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);
            RegistrationServices regSrv = new RegistrationServices();
            regSrv.UnregisterAssembly(base.GetType().Assembly);
        }
    }
}

In my setup project, I added "Primary output from ComplianceManager (Active)" to both the Install and Uninstall Custom Actions. While installation occurs, today's date does not get written to the InstallDate key that is created. In fact, nothing gets written, so I suspect this method is not being called. Any recommendations as to how to call it?
 
Wish I could edit a message I already posted. Here's the most current version of the Installer1.cs file:

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace ComplianceManager
{
    [RunInstaller(true)]
    public partial class Installer1 : Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            RegistrationServices regSrv = new RegistrationServices();
            regSrv.RegisterAssembly(base.GetType().Assembly,
              AssemblyRegistrationFlags.SetCodeBase);
            RegistryKey regKey = Registry.CurrentUser;
            regKey.OpenSubKey("Software\\Vianney Software\\Compliance Manager");
            regKey.SetValue("InstallDate", DateTime.Now.ToString());
        }
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);
            RegistrationServices regSrv = new RegistrationServices();
            regSrv.UnregisterAssembly(base.GetType().Assembly);
        }
    }
}
 
at a glance nothing looks out of place, but i don't have much experience writing to the registry. could it be the path is incorrect and therefore the value is written some place else in the registry?

Jason Meckley
Senior Programmer

faq855-7190
faq732-7259
My Blog
 
You may need to Create (rather than Open) the sub-key?

In the past, I've (successfully) used something like the following (although not part of an installation routine):


RegistryKey keyMain = Registry.CurrentUser.CreateSubKey(_mainKey);
String key = _subKeyCMan;

using (RegistryKey subKey = keyMain.CreateSubKey(key))
{
subKey.SetValue(_nameDate,
dateValue,
RegistryValueKind.String);
}

where (having substituted your names etc. for my details):

_mainKey = "Software\\Vianney Software"
_subKeyCman = "Compliance Manager"
_nameDate = "InstallDate"
dateValue = DateTime.Now.ToString()
 
Jason and DadsDad, thank you for the replies. Dan, I create HKLM\Software\Manufacturer\AppName and two string values of InstallDate and TrialPeriod using Visual Studio's View > Editor > Registry during the setup project.

Jason, you were partially right: I was defining regKey as Registry.CurrentUser instead of Registry.LocalMachine -- a typo that obviously had run-time implications! I do love those typos...

Having changed that, I ran into a version of the error 1001 (I forget which one). Following some advice I found, I added Primary output to both the Install and Commit custom actions. However, at run-time of the install package, I'm now running into a permissions error when trying to set the value of the key: "Error 1001. Attempted to perform an unauthorized application." This was a surprise, since I thought the installer ran with elevated permissions in order to create the initial registry keys.

My app.manifest file is currently set to "asInvoker." I don't really want to change that, but I do need to edit that key. I'll continue to research. In the meantime, any suggestions?

For anyone's reference, here's my revised Installer1.cs:

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Win32;


namespace ComplianceManager
{
    [RunInstaller(true)]
    public partial class Installer1 : Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            RegistrationServices regSrv = new RegistrationServices();
            regSrv.RegisterAssembly(base.GetType().Assembly,
              AssemblyRegistrationFlags.SetCodeBase);
            RegistryKey regKey = Registry.LocalMachine;
            regKey.OpenSubKey("Software\\Vianney Software\\Compliance Manager");
            regKey.SetValue("InstallDate", DateTime.Now.ToString());
        }
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top