2

Hello guys I'm new to the forum also programming and need some help about a project.

So I recently start developing a program that firstly must add its path at the end of Registry => Environment => Path. For this job I created project (MainLogic) which contain a class (Program) that do the job, Installer Class that contains this events below and configured Setup Project. SOURCE

    public InstallerClass1()            
    {            
        this.Committed += InstallerClass1_Committed;
        this.Committing += InstallerClass1_Committing;
    }

    private void InstallerClass1_Committing(object sender, InstallEventArgs e)
    {
        //Console.WriteLine("");
        //Console.WriteLine("Committing Event occurred.");
        //Console.WriteLine("");
    }

    private void InstallerClass1_Committed(object sender, InstallEventArgs e)
    {
            Directory.SetCurrentDirectory(Path.GetDirectoryName
            (Assembly.GetExecutingAssembly().Location));
            Process.Start(Path.GetDirectoryName(
              Assembly.GetExecutingAssembly().Location) + "\\MainLogic.exe");
    }  

The program was installed correctly but MainLogic.exe file I call after installation cause an error and can't start. The exception is Null Reference at MainLogic.Program.Main(String[] args)

Here is a picture for better understanding -

enter image description here

Is there a way to avoid that exception or could you offer me another that will work.

*** Here what i found. I can execute creating and typing in to file. Writing on the console. Probably a lot of other stuff without problem. But when try to execute this peace of code which actually I have to use...

    Registry.CurrentUser.OpenSubKey("Pass Key", RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue("Finaly", "Done");
    Registry.CurrentUser.Close();

...the exception I described above occurs. Suggestions?


So the main reason for all those "exercises" is because I want to implement ffmpeg in my application. I guess you are hear about ffmpeg (a video/audio processing program that works in command prompt). So what I'm working on is to implement it in my project for mp3 extracting from video files but I wanna make it more user friendly so the user can pass commands through GUI and from there my code should do the other job. So ffmpeg works through command prompt (I know there is a wrappers but I'm not very satisfied with what read about) but firstly you have to add his path to Path's value in the registry. And here's where my problem came from.

Maybe it's sounds stupid for you but you know.. when you start something make it all the way.

18
  • 2
    Registry can be manipulated using Setup itself. Commented Aug 10, 2016 at 11:19
  • 1
    And it should be, as the program will probably not run with admin rights after the setup is done. Commented Aug 10, 2016 at 11:20
  • The exception is thrown inside of your main. Look what you do there. Commented Aug 10, 2016 at 11:20
  • @SarveshMishra It is true but you can only add new Keys and Values there. What I want to do is to edit the Path's value data with the currently installed program path which I get in the Installer class. With the way you suggested operation seems to be impossible. Commented Aug 10, 2016 at 11:37
  • @ThorstenDittmar so is there a way to run it as administrator in Committed event? Commented Aug 10, 2016 at 11:40

1 Answer 1

1

If course you can just add exception handling and see what goes wrong but you don`t neet that anyway. Try to set the registry key directly in your Installer

[RunInstaller(true)]
public partial class Installer1 : Installer
{
    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);

        const string key_path = "SOFTWARE\\YourCompany\\YourApplication";
        const string key_value_name = "InstallationDirectory";

        RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree);

        if (key == null)
        {
            key = Registry.LocalMachine.CreateSubKey(key_path);
        }

        string tgt_dir = "someDirectory";

        key.SetValue(key_value_name, tgt_dir);

    }

if you want to alter the path enironment variables set the key there. You can simply add a new variable or look for an exiting one (including the value) for example with Registry.GetValue MSDN-Link

User Variables

HKEY_CURRENT_USER\Environment

System Variables

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
Sign up to request clarification or add additional context in comments.

13 Comments

What if I want want to inspect the MSI to see what registry changes it will do?
You mean if the uinstaller wants to check for existing reg values? You can easily do that.. or do you mean something different?
By Inspection of MSI, I mean analyzing the content of MSI. If registry is manipulated in custom actions, these can't be detected in advance. And if something can be done transparently and easily in simpler way then why complicate the process.
@MarcWittmann I tried your way but more simplified like this Registry.CurrentUser.OpenSubKey("Pass Key", Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue("Finaly", "Done"); Again thrown an exception but this time program didn't finish instaling. Tried same thing in the Commit method = same result.
just include a try catch before your registry processing and messageBox where the nullPointer comes from yes, depending on framework version you can simplyfy it.. just wanted to give you a hint
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.