How to Automatically start Application

微笑、不失礼 提交于 2019-12-11 17:38:22

问题


I work on C#.recently i work on Tcp server-client .I write a client application .want it's start automatically when client start os .Actually i have an exe,want it active when user start his computer.What i need to do?Thanks.if have any query plz ask.


回答1:


There are many ways that you can make an application start at run time.

For a list of locations. Check this article

To summarize they are

Start->Programs->StartUp folder

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run




回答2:


Add following code on your program first page....

    public string path;
    public string fileName;
    public void GetExeLocation()
    {
        path = System.Reflection.Assembly.GetEntryAssembly().Location; // for getting the location of exe file ( it can change when you change the location of exe)
        fileName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; // for getting the name of exe file( it can change when you change the name of exe)
        StartExeWhenPcStartup(fileName,path); // start the exe autometically when computer is stared.
    }

    public void StartExeWhenPcStartup(string filename,string filepath)
    {
        Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        key.SetValue(filename, filepath);
    }



回答3:


Basically, there are two options:

  • Create a shortcut to your program in the Startup folder of your start menu
  • Create an entry in the registry in the Run key



回答4:


The Window Autostart Folder can be very useful. I normally put my applications there.




回答5:


Making your server a windows service is a better option. This way even if the no one is logged on to the computer your program will start and run. Generally, services are a better choice for server applications requiring to run on OS startup.

You can read about how to create a service in C# in the following article



来源:https://stackoverflow.com/questions/5894607/how-to-automatically-start-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!