2 不同的bat文件在vb.net代码中相互干扰
2 Diffrent bat files interfer with eachothers in vb.net code
我有一个软件需要在离线机器上进行保护。
我的问题如下
1-经过一些程序检查运行我的应用程序是否有许可证的机器,如果没有则应用程序通过此代码自行删除
Private Sub SelfDestruct()
Dim procDestruct As Process = New Process()
Dim strName As String = "destruct.bat"
Dim strPath As String = Path.Combine _
(Directory.GetCurrentDirectory(), strName)
Dim strExe As String = New _
FileInfo(Application.ExecutablePath).Name
Dim swDestruct As StreamWriter = New StreamWriter(strPath)
swDestruct.WriteLine("attrib """ & strExe & """" &
" -a -s -r -h")
swDestruct.WriteLine(":Repeat")
swDestruct.WriteLine("del " & """" & strExe & """")
swDestruct.WriteLine("if exist """ & strExe & """" &
" goto Repeat")
swDestruct.WriteLine("del """ & strName & """")
swDestruct.Close()
procDestruct.StartInfo.FileName = "destruct.bat"
procDestruct.StartInfo.CreateNoWindow = True
procDestruct.StartInfo.UseShellExecute = False
Try
procDestruct.Start()
Catch ex As Exception
Close()
End Try
End Sub
它工作了 4 年,直到我发现我需要添加另一种方法来更改机器名称,唯一有效并实际更改名称的方法是从 bat 文件中进行操作。(这里来冲突)
Private Sub PCRename()
Dim Renameact As Process = New Process()
Dim newname As String = "rename.bat"
Dim strName As String = "rename.bat"
Dim strPath As String = Path.Combine _
(Directory.GetCurrentDirectory(), strName)
Dim Renametext As StreamWriter = New StreamWriter(strPath)
Dim rename As String = "wmic computersystem where name=""%computername%"" call rename name="
Dim source As New System.Text.StringBuilder
Renametext.WriteLine("@echo off")
Renametext.WriteLine(rename + """" + newname + """")
Renametext.WriteLine("shutdown /r -t 10")
Renametext.WriteLine("del """ & strName & """")
Renametext.Close()
Renameact.StartInfo.FileName = "rename.bat"
Renameact.StartInfo.CreateNoWindow = True
Renameact.StartInfo.UseShellExecute = False
MessageBox.Show("Computer will restart to complete the setup", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Try
Renameact.Start()
Application.ExitThread()
Application.Exit()
Catch ex As Exception
Close()
End Try
End Sub
现在每次更改名称时应用程序都会自行删除,即使我没有调用“SelfDestruct”也是如此
我尝试了代码,但在注释完“SelfDestruct”的完整代码后,当我取消注释“SelfDestruct”并再次测试问题时,应用程序没有自行删除,应用程序仍然自行删除。
我用谷歌搜索了 2 天,但一无所获。
有没有办法分离方法或清理 StreamWriter(我试过 Flush() 和 Dispose(),仍然没有用)。
谢谢。
出于某种原因,这一行导致我的应用程序被删除
Renametext.WriteLine("del """ & strName & """")
当我删除它时,应用程序保留但 rename.bat 文件,我想删除它,这样就不会有人错误地启动 bat 文件。
为了最终解决这个问题,我从重命名方法中删除了以下行
Renametext.WriteLine("del """ & strName & """")
然后在调用方法后添加这一行
Exit Sub
然后使用此代码在下次启动时删除 rename.bat 文件
Dim Filenames As String = "rename.bat"
Dim FilePath As String = "" & AppDomain.CurrentDomain.BaseDirectory & "" & Filenames & ""
If System.IO.File.Exists(FilePath) = True Then
System.IO.File.Delete(FilePath)
End If
我有一个软件需要在离线机器上进行保护。 我的问题如下 1-经过一些程序检查运行我的应用程序是否有许可证的机器,如果没有则应用程序通过此代码自行删除
Private Sub SelfDestruct()
Dim procDestruct As Process = New Process()
Dim strName As String = "destruct.bat"
Dim strPath As String = Path.Combine _
(Directory.GetCurrentDirectory(), strName)
Dim strExe As String = New _
FileInfo(Application.ExecutablePath).Name
Dim swDestruct As StreamWriter = New StreamWriter(strPath)
swDestruct.WriteLine("attrib """ & strExe & """" &
" -a -s -r -h")
swDestruct.WriteLine(":Repeat")
swDestruct.WriteLine("del " & """" & strExe & """")
swDestruct.WriteLine("if exist """ & strExe & """" &
" goto Repeat")
swDestruct.WriteLine("del """ & strName & """")
swDestruct.Close()
procDestruct.StartInfo.FileName = "destruct.bat"
procDestruct.StartInfo.CreateNoWindow = True
procDestruct.StartInfo.UseShellExecute = False
Try
procDestruct.Start()
Catch ex As Exception
Close()
End Try
End Sub
它工作了 4 年,直到我发现我需要添加另一种方法来更改机器名称,唯一有效并实际更改名称的方法是从 bat 文件中进行操作。(这里来冲突)
Private Sub PCRename()
Dim Renameact As Process = New Process()
Dim newname As String = "rename.bat"
Dim strName As String = "rename.bat"
Dim strPath As String = Path.Combine _
(Directory.GetCurrentDirectory(), strName)
Dim Renametext As StreamWriter = New StreamWriter(strPath)
Dim rename As String = "wmic computersystem where name=""%computername%"" call rename name="
Dim source As New System.Text.StringBuilder
Renametext.WriteLine("@echo off")
Renametext.WriteLine(rename + """" + newname + """")
Renametext.WriteLine("shutdown /r -t 10")
Renametext.WriteLine("del """ & strName & """")
Renametext.Close()
Renameact.StartInfo.FileName = "rename.bat"
Renameact.StartInfo.CreateNoWindow = True
Renameact.StartInfo.UseShellExecute = False
MessageBox.Show("Computer will restart to complete the setup", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Try
Renameact.Start()
Application.ExitThread()
Application.Exit()
Catch ex As Exception
Close()
End Try
End Sub
现在每次更改名称时应用程序都会自行删除,即使我没有调用“SelfDestruct”也是如此
我尝试了代码,但在注释完“SelfDestruct”的完整代码后,当我取消注释“SelfDestruct”并再次测试问题时,应用程序没有自行删除,应用程序仍然自行删除。
我用谷歌搜索了 2 天,但一无所获。
有没有办法分离方法或清理 StreamWriter(我试过 Flush() 和 Dispose(),仍然没有用)。
谢谢。
出于某种原因,这一行导致我的应用程序被删除
Renametext.WriteLine("del """ & strName & """")
当我删除它时,应用程序保留但 rename.bat 文件,我想删除它,这样就不会有人错误地启动 bat 文件。
为了最终解决这个问题,我从重命名方法中删除了以下行
Renametext.WriteLine("del """ & strName & """")
然后在调用方法后添加这一行
Exit Sub
然后使用此代码在下次启动时删除 rename.bat 文件
Dim Filenames As String = "rename.bat"
Dim FilePath As String = "" & AppDomain.CurrentDomain.BaseDirectory & "" & Filenames & ""
If System.IO.File.Exists(FilePath) = True Then
System.IO.File.Delete(FilePath)
End If