c#如何检测windows窗体应用程序在计算机启动后第一次(启动时)运行?

c#如何检测windows窗体应用程序在计算机启动后第一次(启动时)运行?,c#,winforms,triggers,startup,launch,C#,Winforms,Triggers,Startup,Launch,我有一个windows窗体应用程序,我使用安装程序包(inno setup)将其添加到windows启动程序中,该程序运行良好,我的应用程序也在启动时启动。 现在我想触发一个函数,在主/基应用程序启动时执行另一个应用程序。我的意思是,不是每次加载表单时,而是应用程序第一次启动时(启动时) 可以这样做吗?编辑:根据OP的评论和理解,下面是一个简单的示例,说明如何获取上次系统启动时间,然后将其存储在应用程序可执行文件根目录下的文本文件中,下次启动应用程序时,文件中存储的DateTime值将用于验证是

我有一个windows窗体应用程序,我使用安装程序包(inno setup)将其添加到windows启动程序中,该程序运行良好,我的应用程序也在启动时启动。 现在我想触发一个函数,在主/基应用程序启动时执行另一个应用程序。我的意思是,不是每次加载表单时,而是应用程序第一次启动时(启动时)
可以这样做吗?

编辑:根据OP的评论和理解,下面是一个简单的示例,说明如何获取上次系统启动时间,然后将其存储在应用程序可执行文件根目录下的文本文件中,下次启动应用程序时,文件中存储的
DateTime
值将用于验证是否需要运行该方法:

private void Form1_Load(object sender, EventArgs e)
{
    var lastAppStartup = GetLastAppStartup();
    var lastBootUpTime = GetLastBootUpTime();

    // If last computer boot up time is greater than last app start up time
    // Store last boot up time for next launch as app startup time
    if (lastBootUpTime.CompareTo(lastAppStartup) > 0)
    {
        AppMethodRunOnceOnStartup();
        File.WriteAllText("lastbootuptime.txt", lastBootUpTime.ToString("yyyy-MM-dd hh:mm:ss"));
    }
}

private DateTime GetLastBootUpTime()
{
    var query = new SelectQuery(@"SELECT LastBootUpTime FROM Win32_OperatingSystem");
    var searcher = new ManagementObjectSearcher(query);
    var result = DateTime.Now;

    foreach (ManagementObject mo in searcher.Get())
    {
        result = ManagementDateTimeConverter
                    .ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
    }

    return result;
}

private DateTime GetLastAppStartup()
{
    var lastAppStartup = File.ReadAllText("lastbootuptime.txt");
    return DateTime
            .ParseExact(lastAppStartup, "yyyy-MM-dd hh:mm:ss", CultureInfo.InvariantCulture);
}

// Run this method only once when computer boot up
private void AppMethodRunOnceOnStartup()
{
    // This method runs only once based on the last system boot up time
}
使用[Form.Load][1]事件,或在应用程序之前从
Program Main
启动一个进程。运行
启动一个进程(您可能正在寻找
Program Main
):

静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
var process=process.Start(“记事本”);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}

[1]: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form.load?view=netcore-3.1no main()是应用程序的开始初始化。我想要一种方法来检测它是否在计算机启动后第一次运行@kshkarin@HarishKumar您的意思是希望WinForms应用程序只运行一次吗?所以用户不能启动它的多个实例?就像这里提到的?不,不是多个实例,如果您不理解问题,很抱歉。简单地说,我想检测应用程序是否第一次运行。重新启动机器时。对于i have _Load()事件,用户可以多次从开始按钮启动它。但我不希望每次应用程序启动时都触发另一个函数starts@HarishKumar启动应用程序时登录到Windows事件日志,现在事件日志将包括上次启动时间和应用程序启动的时间。下一步是在每次应用程序启动时查询上次启动时间,并将其与应用程序记录的上次事件日志时间戳进行比较。从应用程序记录事件-获取上次启动时间-
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        var process = Process.Start("notepad");
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}