I have a windows form application which i added in windows startup program using installer package (inno setup) which is working fine and my application launches on the start up too. Now i want to trigger a function which execute another application on the startup of main/base application. I mean not every time when a form loads but only the first time when the application launch(on start up) is it possible to do so?
EDIT: based on the OP comments and understanding, here's a simple example of how to get the last system boot up time and then storing it in a text file at the root of the app executable, next time the app is launched the DateTime
value stored in the file will be used to verify if there's a need to run the method:
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
}
Either use the [Form.Load][1] event, or from
Program Main
before
Application.Run
to launch a process (probably
Program Main
is what you're looking for):
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()); } }
[1]: https://learn.microsoft.com/en-us/do.net/api/system.windows.forms.form.load?view.netcore-3.1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.