Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#,WPF中注册为启动程序后在后台运行的原因_C#_Wpf_Registry_Startup - Fatal编程技术网

在C#,WPF中注册为启动程序后在后台运行的原因

在C#,WPF中注册为启动程序后在后台运行的原因,c#,wpf,registry,startup,C#,Wpf,Registry,Startup,在MainWindow.xaml.cs中 public partial class MainWindow : Window { public static bool flagBtnConnecting_Clicked = true; IConfiguration appsettings = new ConfigurationBuilder() .AddJsonFile("appsettings.json"

在MainWindow.xaml.cs中

public partial class MainWindow : Window
    {
        public static bool flagBtnConnecting_Clicked = true;
        IConfiguration appsettings = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();
        public static NotifyIcon Trayicon = new NotifyIcon();

        public MainWindow()
        {
            InitializeComponent();
            Tray_Init();
            
            Loaded += InitMainWindow;
        }
在App.xaml.cs中

public partial class App : Application
    {
        
        public static MainWindow mainWindow = new MainWindow();
        public static View.StartupSetWindow startupSetWindow = new View.StartupSetWindow();
        public void Application_Startup(object sender, StartupEventArgs e)
        {

            try
            {
                InitLog();
                SLog.Write(ModuleType.SYSTEM, Environment.CurrentDirectory);
                SLog.Write(ModuleType.SYSTEM, "Started");
                Task.Run(() => ConsoleRunning());
                mainWindow.Activate();
            }
            catch (Exception)
            {
                SLog.Exception("VCS control service has the problem.");
                return;
            }
            finally
            {

            }
        }
在AddStartUp方法中(在StartupSetWindow.xaml.cs中)

在debug或release中,我制作的托盘图标和UI运行良好。但是,如上面的代码所示,即使我自己注册了启动程序并在注册表中进行了检查,托盘图标仍然不可见,只有任务管理器的进程可见。我检查的结果是,它无法正常工作,因为它无法与其他程序通信。运行bin\Debug\netcoreapp3.1\VCSControlService.exe是否正确?我不知道为什么它只是在进行中

作为参考,使用'mainWindow.Activate();'用于在第一次运行时仅显示托盘图标。我曾经尝试过使用“mainWindow.Show()”,但什么也没发生

private void btnAddStartUp_Click(object sender, EventArgs e)
        {
            try
            {
                RegistryKey runRegKey = 
                Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                
                if (runRegKey.GetValue("vcstest") == null){
                    runRegKey.SetValue("vcstest", Environment.CurrentDirectory + "\\" + 
                    AppDomain.CurrentDomain.FriendlyName );
             }
             MessageBox.Show("Add Startup Success");
             SLog.Write(ModuleType.SYSTEM, "Add Startup Success");
            }
            catch (Exception)
            {
                MessageBox.Show("Add Startup Fail");
                SLog.Exception("btnAddStartUp_Click");
            }
        }