.NET使c#程序在后台和启动时像反病毒一样运行

时间:2021-10-09 01:25:20

For a big project I am going to be making a C# Program which does a variety of things that a user specified, there is a user interface, that will be accessible from a System Icon, and ever present logo on the screen.

对于一个大项目,我将制作一个c#程序,它可以做用户指定的各种事情,有一个用户界面,可以通过系统图标访问,并且可以在屏幕上显示logo。

I want the program to run at start up and be there in the background until shutdown, such as many common antivirus/malware programs out there today.

我希望程序在启动时运行,并在后台一直运行到关闭,比如现在很多常见的反病毒/恶意软件程序。

I also want the program to be installed in order to run.

我还希望安装这个程序以便运行。

I know there's alot here. I know how to get the program to run at startup (startup folder), but like with AntiVirus I want it to always be there, and not just be in the startup folder...

我知道这里有很多。我知道如何让程序在startup (startup文件夹)中运行,但就像使用杀毒软件一样,我希望它总是在那里,而不只是在startup文件夹中……

I originally thought this was a service, but I am not too sure now, and I am having a nightmare getting my head around it all. What is the best way to do this ? The GUI is a must.

我原本以为这是一项服务,但我现在不太确定,而且我正在做一个噩梦,把它搞得一团糟。最好的方法是什么?GUI是必须的。

Would appreciate any and all answers, tips and suggestions.

如有任何答案、提示和建议,将不胜感激。

Thanks in advance.

提前谢谢。

2 个解决方案

#1


8  

Anti-virus programs and similar system tray applications sometimes or often have 2 components. There's a service that starts with the system, and a separate system tray application in the startup folder that interacts with the server to provider user access to service functionality when a user is logged in to a desktop. One of the questions you'll have to answer is whether you want anything running when nobody is logged in. If so, you'll want a service component. If not, you can maybe put everything in one application that creates a system tray icon. To put an icon in the system tray from a .NET application, refer to http://www.developer.com/net/net/article.php/3336751/C-Tip-Placing-Your-C-Application-in-the-System-Tray.htm

防病毒程序和类似的系统托盘应用程序有时或经常有两个组件。有一个服务从系统开始,在启动文件夹中有一个单独的系统托盘应用程序,当用户登录到桌面时,它与服务器交互以提供用户对服务功能的访问。您必须回答的一个问题是,是否希望在没有人登录时运行任何东西。如果是这样,您将需要一个服务组件。如果不是,您可以将所有内容都放在一个应用程序中,该应用程序将创建一个系统托盘图标。要在.NET应用程序的系统托盘中放置图标,请参考http://www.developer.com/net/article.php/3336751/c - tip - place -你的- c - application -in- system tray.htm

If I understand/recall correctly, there's a .NET component designed for putting icons in the system tray.

如果我没记错的话,有一个。net组件是为在系统托盘中放置图标而设计的。

#2


0  

Here's the sample I mentioned above.

这是我上面提到的样品。

This code is located in the TaskbarUtility, and all events throughout the application create new Forms through here.

此代码位于TaskbarUtility中,应用程序中的所有事件都通过这里创建新的表单。

I'm not sure if this is the "right" way to do something like this, and I don't intend to be thread-jacking, but I figured I might as well share. :)

我不确定这是否是做这样事情的“正确”方式,我也不打算做线程劫持,但我想我还是分享一下吧。:)

    List<CommonFormBase> activeWindows = new List<CommonFormBase>();

    public void LaunchApplication(ApplicationWindowType formTypeToLaunch)
    {
        CommonFormBase tempForm;
        switch (formTypeToLaunch)
        {
            //implement code to create a new form here
        }

        activeWindows.Add(tempForm);
        tempForm.Name = "UniqueName:" + System.DateTime.Now;
        tempForm.FormClosed += new FormClosedEventHandler(tempForm_FormClosed);
        tempForm.Show();
    }

    void tempForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (sender is CommonFormBase)
        {
            //Get the index of the selected form
            int index = -1;
            for (int i = 0; i <= this.activeWindows.Count - 1; i++)
            {
                if (this.activeWindows[i].Name == ((Form)sender).Name)
                    index = i;
            }

            //Remove the selected form from the list
            if (index >= 0)
                activeWindows.RemoveAt(index);

            //Close the TaskbarUtility if no remaining windows
            //  and user choose to exit when none remain
            if (this.activeWindows.Count == 0 && CloseWhenNoWindowsRemain)
            {
                this.Close();
            }
        }
    }

Hope that makes sense.

希望是有意义的。

The assumption is that not all users want the app to run all the time, so this will close the entire program when they enable the option.

假设并不是所有用户都希望应用程序一直运行,所以当他们启用这个选项时,整个程序就会关闭。

#1


8  

Anti-virus programs and similar system tray applications sometimes or often have 2 components. There's a service that starts with the system, and a separate system tray application in the startup folder that interacts with the server to provider user access to service functionality when a user is logged in to a desktop. One of the questions you'll have to answer is whether you want anything running when nobody is logged in. If so, you'll want a service component. If not, you can maybe put everything in one application that creates a system tray icon. To put an icon in the system tray from a .NET application, refer to http://www.developer.com/net/net/article.php/3336751/C-Tip-Placing-Your-C-Application-in-the-System-Tray.htm

防病毒程序和类似的系统托盘应用程序有时或经常有两个组件。有一个服务从系统开始,在启动文件夹中有一个单独的系统托盘应用程序,当用户登录到桌面时,它与服务器交互以提供用户对服务功能的访问。您必须回答的一个问题是,是否希望在没有人登录时运行任何东西。如果是这样,您将需要一个服务组件。如果不是,您可以将所有内容都放在一个应用程序中,该应用程序将创建一个系统托盘图标。要在.NET应用程序的系统托盘中放置图标,请参考http://www.developer.com/net/article.php/3336751/c - tip - place -你的- c - application -in- system tray.htm

If I understand/recall correctly, there's a .NET component designed for putting icons in the system tray.

如果我没记错的话,有一个。net组件是为在系统托盘中放置图标而设计的。

#2


0  

Here's the sample I mentioned above.

这是我上面提到的样品。

This code is located in the TaskbarUtility, and all events throughout the application create new Forms through here.

此代码位于TaskbarUtility中,应用程序中的所有事件都通过这里创建新的表单。

I'm not sure if this is the "right" way to do something like this, and I don't intend to be thread-jacking, but I figured I might as well share. :)

我不确定这是否是做这样事情的“正确”方式,我也不打算做线程劫持,但我想我还是分享一下吧。:)

    List<CommonFormBase> activeWindows = new List<CommonFormBase>();

    public void LaunchApplication(ApplicationWindowType formTypeToLaunch)
    {
        CommonFormBase tempForm;
        switch (formTypeToLaunch)
        {
            //implement code to create a new form here
        }

        activeWindows.Add(tempForm);
        tempForm.Name = "UniqueName:" + System.DateTime.Now;
        tempForm.FormClosed += new FormClosedEventHandler(tempForm_FormClosed);
        tempForm.Show();
    }

    void tempForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (sender is CommonFormBase)
        {
            //Get the index of the selected form
            int index = -1;
            for (int i = 0; i <= this.activeWindows.Count - 1; i++)
            {
                if (this.activeWindows[i].Name == ((Form)sender).Name)
                    index = i;
            }

            //Remove the selected form from the list
            if (index >= 0)
                activeWindows.RemoveAt(index);

            //Close the TaskbarUtility if no remaining windows
            //  and user choose to exit when none remain
            if (this.activeWindows.Count == 0 && CloseWhenNoWindowsRemain)
            {
                this.Close();
            }
        }
    }

Hope that makes sense.

希望是有意义的。

The assumption is that not all users want the app to run all the time, so this will close the entire program when they enable the option.

假设并不是所有用户都希望应用程序一直运行,所以当他们启用这个选项时,整个程序就会关闭。