0

I registered some WPF from as startup like this

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Assembly curAssembly = Assembly.GetExecutingAssembly();
key.SetValue(curAssembly.GetName().Name, Process.GetCurrentProcess().MainModule.FileName);

and register NotifyIcon like this

HandyOrderPOSIcon = new System.Windows.Forms.NotifyIcon();
HandyOrderPOSIcon.Icon = new System.Drawing.Icon("Icon.ico");
HandyOrderPOSIcon.Visible = true;
HandyOrderPOSIcon.ContextMenuStrip = menuStrip;
HandyOrderPOSIcon.MouseClick += delegate (object sender, System.Windows.Forms.MouseEventArgs e) {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
          menuStrip.Show();
        }
      };

When restart Windows, it throw a Exception like this

fileio exception

but when execute this program manually, it works without this exception.

JY Lee
  • 45
  • 6
  • looks like the working directory is different from when you run the executable manually , check out this thread [](https://stackoverflow.com/questions/2822951/use-registry-to-startup-a-program-and-also-change-the-current-working-directory) – yassinMi Jul 01 '22 at 04:31
  • 1
    It would be better to load icon file from resources. https://stackoverflow.com/a/74671/3137337 – emoacht Jul 01 '22 at 05:16
  • @emoacht I resolved this problem like this `HandyOrderPOSIcon = new System.Windows.Forms.NotifyIcon(); Stream iconStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProgramNamespace.Icon.ico"); HandyOrderPOSIcon.Icon = new System.Drawing.Icon(iconStream)` Thx! – JY Lee Jul 01 '22 at 06:46

0 Answers0