I need to have two ways to start my program: the usual start with the display of the form, and the hidden start without displaying the form - when you start Windows. In both cases, a tray icon is created. I did this: if the number of parameters passed at startup is more than zero, then we start it secretly. It works as it should. Next, I made it so that the program automatically started when Windows was started by placing the appropriate line in the registry

Open code

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace RZD_Bonus_Keep { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { Application.SetCompatibleTextRenderingDefault(false); // создаём Ρ‚Ρ€Π΅ΠΉ ΠΈ всё для Π½Π΅Π³ΠΎ // контСкстноС мСню var contmenu = new System.Windows.Forms.ContextMenu(); var menuItem1 = new System.Windows.Forms.MenuItem(); menuItem1.Text = "RZD Bonus Keep"; var menuItemSplitter = new System.Windows.Forms.MenuItem(); menuItemSplitter.Text = "-"; var menuItem2 = new System.Windows.Forms.MenuItem(); menuItem2.Text = "ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΎΠΊΠ½ΠΎ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹"; menuItem2.Click += new System.EventHandler(menuItemClick); var menuItem3 = new System.Windows.Forms.MenuItem(); menuItem3.Text = "О ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ΅"; var menuItem4 = new System.Windows.Forms.MenuItem(); menuItem4.Text = "Π’Ρ‹Ρ…ΠΎΠ΄"; menuItem4.Click += new System.EventHandler(menuItem4Click); contmenu.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] { menuItem1, menuItemSplitter, menuItem2, menuItem3, menuItemSplitter, menuItem4 } ); // всё ΠΏΠΎ Ρ‚Ρ€Π΅ΡŽ NotifyIcon tray = new System.Windows.Forms.NotifyIcon(); tray.Visible = true; tray.Icon = new System.Drawing.Icon("ico.ico"); tray.Text = "RZD Bonus Keeper"; tray.ContextMenu = contmenu; tray.MouseClick += new MouseEventHandler(trayClick); if (args.Length>0) { // запуск Π±Π΅Π· ΠΎΠΊΠ½Π° Application.Run(); } else { // Запуск Π² графичСском Ρ€Π΅ΠΆΠΈΠΌΠ΅ fmain = new fMain(); Application.Run(fmain); } } static fMain fmain; private static void trayClick(object Sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) GoGraphicView(); } // ΠΊΠ»ΠΈΠΊ ΠΏΠΎ контСкстному мСню ΠžΡ‚ΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡƒ private static void menuItemClick(object Sender, EventArgs e) { GoGraphicView(); } private static void menuItem4Click(object Sender, EventArgs e) { if (MessageBox.Show("Π’Ρ‹ ΡƒΠ²Π΅Ρ€Π΅Π½Ρ‹, Ρ‡Ρ‚ΠΎ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ Π·Π°ΠΊΡ€Ρ‹Ρ‚ΡŒ ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) Application.Exit(); } static void GoGraphicView() { // Запуск Π² графичСском Ρ€Π΅ΠΆΠΈΠΌΠ΅ // Ρ„ΠΎΡ€ΠΌΠ° ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΡƒΠΆΠ΅ Π·Π°ΠΏΡƒΡ‰Π΅Π½Π° if (fmain == null) { Application.EnableVisualStyles(); fmain = new fMain(); fmain.Show(); } else { fmain.WindowState = FormWindowState.Maximized; } } } } 

When you try to start automatically with the start of Windows, we get the error:

 <?xml version="1.0" encoding="UTF-16"?> -<WERReportMetadata> -<OSVersionInformation> <WindowsNTVersion>6.1</WindowsNTVersion> <Build>7601 Service Pack 1</Build> <Product>(0x30): Windows 7 Professional</Product> <Edition>Professional</Edition> <BuildString>7601.18798.amd64fre.win7sp1_gdr.150316-1654</BuildString> <Revision>1130</Revision> <Flavor>Multiprocessor Free</Flavor> <Architecture>X64</Architecture> <LCID>1049</LCID> </OSVersionInformation> -<ProblemSignatures> <EventType>CLR20r3</EventType> <Parameter0>RZD-Bonus Keep.exe</Parameter0> <Parameter1>1.0.0.0</Parameter1> <Parameter2>587fa65e</Parameter2> <Parameter3>mscorlib</Parameter3> <Parameter4>4.6.1055.0</Parameter4> <Parameter5>563c0eac</Parameter5> <Parameter6>157f</Parameter6> <Parameter7>ca</Parameter7> <Parameter8>System.IO.FileNotFoundException</Parameter8> </ProblemSignatures> -<DynamicSignatures> <Parameter1>6.1.7601.2.1.0.256.48</Parameter1> <Parameter2>1049</Parameter2> <Parameter22>0a9e</Parameter22> <Parameter23>0a9e372d3b4ad19135b953a78882e789</Parameter23> <Parameter24>0a9e</Parameter24> <Parameter25>0a9e372d3b4ad19135b953a78882e789</Parameter25> </DynamicSignatures> -<SystemInformation> <MID>7BA09EA0-B8C7-47C2-AF7D-C48FE21EE57D</MID> <SystemManufacturer>To be filled by OEM</SystemManufacturer> <SystemProductName>To be filled by OEM</SystemProductName> <BIOSVersion>2501</BIOSVersion> </SystemInformation> </WERReportMetadata> 

On the Internet did not find a solution. empirically realized that, if you commented out a line of code tray.Icon = new System.Drawing.Icon ("ico.ico"); then the error does not occur, however, respectively, and the tray icon does not come out.

In references at System.Drawing, copylocal is true.

What to make that the error did not arise where to dig?

  • Try changing the icon path to Application.StartupPath + "\\ico.ico" - Raider

1 answer 1

I solved the problem as follows: I used properties.resources