应用程序将不会加入后的NotifyIcon在启动时启动 [英] application won't start on startup after adding a notifyicon

查看:187
本文介绍了应用程序将不会加入后的NotifyIcon在启动时启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是确定的工作,它会之前启动运行。
我增加了一个通知图标,并在我的代码,也有一些地方,此图标changes.I添加了所有必需的图标在我的应用程序的根文件夹,一切工作正常使用的图标,除的开机启动我的应用程序。
我可以在注册表中(我的意思是一切是一样的,当我的应用程序在启动时启动正常),但我的应用程序的运行部分看到我的应用程序的地址不会在启动时再跑。
对我的事什么建议吗?
PS:我想我应该解释一下我的工作一点点,我写了一个小片应用程序具有完全相同的问题。

my app was working ok and it would execute on startup before. I added a notify icon and in my code,there are some places that this icon changes.I added all required icons in the root folder of my app,and everything is working fine with the icons,except the startup boot of my app. I can see my app's address in the "run" part of the registry(I mean everything is the same as when my app booted at startup properly).but my app won't run at startup anymore. any advice on my matter? PS:I thought I should explain my work a little bit and I wrote a little piece of app that has the exact same problem

    public Icon[] icons = new Icon[2] { new Icon("icon1.ico"), new Icon("icon2.ico") };
    public int counter = 0;


    private void button1_Click(object sender, EventArgs e)
    {
        notifyIcon1.Visible = true;
        timer1.Start();

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        counter %= 2;
        notifyIcon1.Icon = icons[counter];
        counter++;



正如你所看到的,应用程序改变的NotifyIcon的图标在每个tick.with这段代码,该应用程序将不startup.but运行,如果我删除该应用程序的iconchanging功能,它实际上将在启动时运行

As you can see,the app changes the icon of the notifyicon in every tick.with this code,the app won't run at startup.but if I remove the iconchanging feature of the app,it will actually run at startup

推荐答案

这需要心理调试,我猜你使用的是他们的相对的路径名加载这些图标。像新图标(foo.ico)。

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

如果默认工作程序的目录设置,你希望这将是这只能正常工作。它通常是,当然,当你从Visual Studio启动程序或桌面快捷方式启动它。但是的不是当你添加它来运行注册表项的。 Environment.CurrentDirectory将另行设置,通常Windows目录中。

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);



但肯定还有比存储图标为文件更好的方法,你可以在你的程序中嵌入它们。项目+属性,资源选项卡。单击箭头上添加资源按钮,添加现有文件并浏览到您的.ico文件。现在的图标嵌入在程序中,你永远不会失去它的轨道,当你在另一台机器上部署你的程序不能忘记复制它。而且代码更简单,以及:

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;

这篇关于应用程序将不会加入后的NotifyIcon在启动时启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆