application won't start on startup after adding a notifyicon

帅比萌擦擦* 提交于 2019-12-06 13:53:16

This requires psychic debugging, I'll guess that you are loading these icons using their relative path name. Something like new Icon("foo.ico").

This can only work correctly if the default working directory of your program is set where you hope it will be. It usually is, certainly when you start your program from Visual Studio or start it from a desktop shortcut. But not when you added it to the Run registry key. Environment.CurrentDirectory will be set elsewhere, typically the Windows directory.

You must always use the full path name of files. An easy way to get that path is:

    var home = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
    var path = System.IO.Path.Combine(home, "foo.ico");
    var icon = new Icon(path);

But there's certainly a better way than storing icons as files, you can embed them in your program. Project + Properties, Resources tab. Click the arrow on the Add Resource button, Add Existing File and navigate to your .ico file. Now the icon is embedded in your program, you'll never lose track of it and can't forget to copy it when you deploy your program on another machine. And the code is simpler as well:

    var icon = Properties.Resources.foo;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!