0

I have a solution with two project files in it. One executable is a windows form application and the other one is a console application. Both executables perform different tasks, however, both need to be run at the same time (only the windows form has to be started). Therefore I added following code to my windows form application:

RegistryKey rkApp =
    Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
    true);

    public Form1()
    {
        if (rkApp.GetValue("somename") == null)
        {
            rkApp.SetValue("somename", Application.ExecutablePath.ToString());


        }

When I now restart the PC, everything's gone... Any ideas why this problem is turning up? Thank you!

P.S.: I'm a complete beginner, please be nice :)

vcsjones
  • 138,677
  • 31
  • 291
  • 286
user3492582
  • 81
  • 1
  • 1
  • 6
  • Why don't you just add the two executables to the startup folder? – Khan Apr 04 '14 at 20:01
  • I meant the processes are not in the task manager anymore @Khan do i need to add both, or just the windows form application? is it not necessary to create a windows service project? – user3492582 Apr 04 '14 at 20:09

3 Answers3

0

You need to create a Windows Service Project and install with a service in windows.

Only a Curious Mind
  • 2,807
  • 23
  • 39
0

If you have a .EXE file, you can add it to Start Up Programms using MSCONFIG from CMD, it's a Non-Coded Solution, but if you want it to attach to register as a service, then create a Windows Service Project.

Ivan Verges
  • 595
  • 3
  • 10
  • 25
  • can i integrate the service in the exe file, or do i then need another extra file? – user3492582 Apr 04 '14 at 20:06
  • What Do You Mean With Integrate The Service? Take a look at this: http://www.vistax64.com/tutorials/79612-startup-programs-enable-disable.html There they show some ways to Add / Remove StartUp Programms. – Ivan Verges Apr 04 '14 at 20:08
0

Use REGEDIT to confirm that the 'somename' was added, also your code has the problem that it only checks 'somename' was previously set, not that the path is correct. If you ran the code in a different path during testing, it would never be updated. Also RegistryKey should be closed after you are finished with it. By rkApp.Close() or by 'using'.

using (RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
    if (rkApp.GetValue("somename") != Application.ExecutablePath.ToString())
    {
        rkApp.SetValue("somename", Application.ExecutablePath.ToString());
    }
}

You say only the form needs to be launched, does the form run the console? What happens?

Anothing possibility is when running a program by the registry, the 'Current Directory' is 'C:\Windows\System32' and not the application path. If you are using code such as:

File.ReadAllText('SomeFile.txt');

It would try opening C:\Windows\System32\SomeFile.txt and not \YourApp\SomeFile.txt, which can be fixed by:

// Reliably get the .EXE directory, this works for both Form and Console applications:
var AssemblyDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

// Option 1 is set on load:
Directory.SetCurrentDirectory(AssemblyDirectory);

// Option 2 is use full paths for anything opening files:
File.ReadAllText(AssemblyDirectory + "\\SomeFile.txt");
WhoIsRich
  • 4,053
  • 1
  • 33
  • 19
  • Yes, the windows form starts the console. Is there a way I can see if new code works, without rebooting always my computer? – user3492582 Apr 04 '14 at 20:55
  • You do not need to reboot, just logoff and logon. – WhoIsRich Apr 04 '14 at 20:56
  • You can also use REGEDIT to copy the current value, and put it in the run box ( Windows Key + R ). – WhoIsRich Apr 04 '14 at 20:58
  • With the new code the main program seems to be running. However, it does not do what it should do and the console application does not open either @WholsRich – user3492582 Apr 04 '14 at 21:18
  • 'does not do' is something for another question. For the console, check your run path: Process.Start(Application.StartupPath + "\\console.exe"); – WhoIsRich Apr 04 '14 at 21:27
  • you got me wrong, I think. The console runs perfectly when I open the executable file. However, when I reboot the PC, the windows form application is in the taskmanager, but simply does not work nor open the console... – user3492582 Apr 04 '14 at 21:34
  • Does the program need the network or depend on something else that needs time to load? Try adding a System.Threading.Thread.Sleep(9000); to Form Load of your application. – WhoIsRich Apr 04 '14 at 22:00
  • ok @WholsRich, I tried your suggestion but it still does not work... :( – user3492582 Apr 05 '14 at 09:47
  • I have updated my answer with another reason your program may fail. – WhoIsRich Apr 05 '14 at 14:01