.net - 使用 Windows 任务计划程序启动程序后如何最小化程序?
问题描述
所以我有一个程序,我想在 Windows 的通知托盘中开始最小化。如果我从开始菜单手动启动它,我有程序可以做到这一点,但我遇到了一个问题,如果我尝试使用任务计划程序启动它,它将不会最小化。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists("enabled") Then
Me.Hide()
Me.WindowState = FormWindowState.Minimized
End If
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Title"
NotifyIcon1.BalloonTipText = "Text"
NotifyIcon1.ShowBalloonTip(50000)
'Me.Hide()
ShowInTaskbar = False
End If
Private Sub enable_Click(sender As Object, e As EventArgs) Handles enable.Click
Using tService As New TaskService()
Dim tDefinition As TaskDefinition = tService.NewTask
tDefinition.RegistrationInfo.Description =
"Test description"
'LogonTrigger'
Dim tLogon As New LogonTrigger()
tLogon.UserId = SystemInformation.UserName
tDefinition.Triggers.Add(tLogon)
tDefinition.Actions.Add(New ExecAction(exePath))
tService.RootFolder.RegisterTaskDefinition("Test",
tDefinition)
End Using
Dim fs As FileStream = File.Create("enabled")
fs.Close()
End Sub
这些是程序启动时使用的代码行,用于安排程序在用户登录时启动。对于任务调度,我使用了 dahall 的 Task Scheduler .NET 包装器,您可以在此处找到:https ://github.com/dahall/taskscheduler
解决方案
最好始终使用文件的完整路径,因为这样您就可以确定它正在查找您希望它查找的位置。
您可以使用以下方式返回的文件名:
IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "yourProgramsDataDirectoryGoesHere", "enabled")
该文件和 .NET 已知的其他 Windows“特殊文件夹”列在Environment.SpecialFolder Enum中。
要检查的另一件事是运行任务的帐户是否有权访问该文件。
[此外,要创建一个更易于使用的文件,IO.File.WriteAllText(fullPathToTheFile, "")
您不必记得调用.Dispose()
FileStream。]
推荐阅读
- mean - 如果我们只知道均值、SD 和 N,我们可以计算数据的范围吗?
- jquery - 如何从 jqXHR 中提取请求?
- shell - 在 Shell 中的每个空行之后打印所有行
- r - 删除 R 中的 NA 以计算累积回报
- kubernetes - 使用 istioctl 和 Istio operator 升级 Istio 1.4.3 到 1.5.6
- php - 特定字符之前的 PHP 粗体字符串
- java - org.springframework.jdbc.CannotGetJdbcConnectionException,嵌套异常是java.sql.SQLException
- javascript - Angular APPINITIALIZER TypeError:未定义不是对象和承诺
- cognos - Cognos 11 - 查询主题之间的过滤器
- c# - 网页中与脚本相关的 HTML 标记的不正确中和(基本 XSS)CWE ID 80