该进程试图写入一个不存在的管道 vb.net

The process tried to write to a nonexistent pipe vb.net

我目前正在 vb.net 中编写一个子程序,它应该在 windows 10 附带的任务调度程序中创建 and/or 擦除任务。创建部分工作正常,但是当我尝试删除任务时,我在 visual studio 控制台中收到此错误:“进程试图写入一个不存在的管道”,并且任务(当然)仍然存在。我一直在寻找解决方案,但找不到类似的东西。这是代码:

Private Sub SimpleButton2_Click(sender As Object, e As EventArgs) Handles sbAutoRun.Click

  Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
  CMDThread.Start()

End Sub

Private Sub CMDAutomate()

  Dim myprocess As New Process
  Dim StartInfo As New System.Diagnostics.ProcessStartInfo
  Dim sTime As String = teAutoRun.Time.ToString("HH:mm")
  Dim sCmdCommand As String = "SCHTASKS /CREATE /SC DAILY /TN ""MyTasks\task"" /TR ""'" & My.Application.Info.DirectoryPath & "\program.exe' Auto"" /ST " & sTime & " /RL HIGHEST"
  Dim sCmdCommand2 As String = "SCHTASKS /DELETE /TN ""MyTasks\task"""
  My.Settings.AutoRun = ceAutoRun.Checked

  StartInfo.FileName = "cmd" 'starts cmd window
  StartInfo.RedirectStandardInput = True
  StartInfo.RedirectStandardOutput = True
  StartInfo.CreateNoWindow = True '<---- if you want to not create a window
  StartInfo.UseShellExecute = False 'required to redirect
  myprocess.StartInfo = StartInfo
  myprocess.Start()
  Dim SR As System.IO.StreamReader = myprocess.StandardOutput
  Dim SW As System.IO.StreamWriter = myprocess.StandardInput
  If My.Settings.AutoRun And Not My.Settings.AutoRunExists Then
    SW.WriteLine(sCmdCommand)
    My.Settings.AutoRunExists = True
    Console.WriteLine(sCmdCommand)
  ElseIf My.Settings.AutoRun And My.Settings.AutoRunExists Then
    SW.WriteLine(sCmdCommand2)
    SW.WriteLine("y")
    SW.WriteLine(sCmdCommand)
  Else
    SW.WriteLine(sCmdCommand2)
    SW.WriteLine("y")
    My.Settings.AutoRunExists = False
    Console.WriteLine(sCmdCommand2)
  End If
  SW.WriteLine("exit") 'exits command prompt window
  SW.Close()
  SR.Close()
  My.Settings.Save()

End Sub

谢谢..

Ps:在 cmd 中手动输入时,这些命令工作正常。

更新:更改了这个

sCmdCommand2 As String = "SCHTASKS /DELETE /TN ""MyTasks\task"""

SW.WriteLine(sCmdCommand2)
SW.WriteLine("y")

然后这样写

sCmdCommand2 As String = "SCHTASKS /DELETE /TN ""MyTasks\task"" /F"

SW.WriteLine(sCmdCommand2)

现在可以正常使用了。