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?
Application.StartupPath + "\\ico.ico"- Raider