VB.Net Self Deletion after 5 days - vb.net

Im have to execute the following code.
Dim Info As ProcessStartInfo = New ProcessStartInfo()
Info.Arguments = "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del """ & Application.ExecutablePath.ToString & """"
Info.WindowStyle = ProcessWindowStyle.Hidden
Info.CreateNoWindow = True
Info.FileName = "cmd.exe"
Process.Start(Info)`
The code will delete the file on execution. How can I code my program so the function is called 5 days after first execution.
Thanks in advance.

you might do the following
1- create a windows service application, check this link to get more information
Developing Windows Service Applications
2- use timer , check this link to get more information
Windows service and timer
hope this will help you

Related

VB.net 2015 Prevent a second CMD instance to show up

Shell("cmd.exe", AppWinStyle.MinimizedFocus)
Thread.Sleep(50)
My.Computer.Keyboard.SendKeys("cd c:\users\Administrator\desktop\captcharuc", True)
My.Computer.Keyboard.SendKeys("{Enter}", True)
Thread.Sleep(50)
My.Computer.Keyboard.SendKeys("tesseract.exe imagen.jpg leerca -psm 7", True)
My.Computer.Keyboard.SendKeys("{Enter}", True)
Thread.Sleep(50)
My.Computer.Keyboard.SendKeys("exit", True)
My.Computer.Keyboard.SendKeys("{Enter}", True)
im streaming commands to a CMD instance, everything works fine but when i execute the tesseract.exe another CMD instance opens to execute such program.
this is the regular behavior of tesseract if i do all those steps manually, a second CMD instance will show up to execute the tesseract commands.
this is part of a windows app that im developing and really dont want that second instance to show up.
I have tried with
Const strCmdText As String = "/c cd c:\users\Administrator\desktop\captcharuc\&tesseract.exe imagen.jpg leerca -psm 7"
Dim startInfo As New ProcessStartInfo("CMD.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.UseShellExecute = False
startInfo.Arguments = strCmdText
Process.Start(startInfo)
and with
Shell("cmd /c """ & "cd c:\users\Administrator\desktop\captcharuc\" & "&tesseract.exe imagen.jpg leerca -psm 7" & """", AppWinStyle.Hide, True)
in the 3 cases the main instance will either run minimized or hidden as intended but the second instance will bring up the tesseract instance.
would really appreciate if i get some help here as I ran out of ideas on how to prevent such event from happening.
thanks in advance
after a good night sleep i came up with a solution to the problem, i did create a .bat file like this
#Echo off
Set _SourcePath=C:\Users\Administrator\Desktop\captcharuc\*.jpg
Set _OutputPath=C:\Users\Administrator\Desktop\captcharuc\
Set _Tesseract="C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"
For %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%leerca
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
and then called it with
Shell("c:\users\Administrator\desktop\captcharuc\tese.bat", AppWinStyle.Hide)
and thats it.. mods feel free to mark this thread as solved. thanks

Install driver inf from Visual Studio Setup project

So.. We have a C# utility application for a usb/serial device. I would like to install the driver during the setup process. I know there are lots of posts about this, and I've gone through many msdn and stackoverflow articles, but I just do not seem to succeed.
The driver is OK, it is a stock Arduino driver, so it is signed and can be installed from windows gui (right click on the inf, install)
I created a visual studio setup project for our applcation, and a VB project for the custom setup actions. The setup copies the driver to the installation folder. The driver installation is performed from the "OnCommitted" method. Here I spawn a process by calling cmd.exe and would like to call some command to install the driver.
First I tried "RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection DefaultInstall 132 ", but it is always fails, although I acquire admin rights for this.
I tried to acquire admin rights at different places:
- I tried to add a "Launch Condition" with the condition "AdminUser"
- I tried to run cmd as admin from VB (Process.Startinfo.Verb = "runas")
none of the above solved my problem.
Then I tried alternative ways to install the driver and InfDefaultInstall.exe is almost fine, but sometimes it fails on some machines, and I don't know why. pnputil.exe always fails for me.
I guess the official way to do this is calling InstallHinfSection as MSDN suggests, and I would prefer to use that. We have to support Windows 7 and above.
Any thoughts on what I do wrong?
Thanks in advance!
here are my VB methods:
Private Sub InstallDriverPNPUtil()
Dim infPath As String = """" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\arduino.inf" + """"
Dim processInfo As New ProcessStartInfo("CMD", "/C pnputil -i -a " + infPath)
processInfo.WindowStyle = ProcessWindowStyle.Hidden
Dim p As System.Diagnostics.Process = Process.Start(processInfo)
p.WaitForExit()
End Sub
Private Sub InstallDriverInfDefaultInstall()
Dim infPath As String = """" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\arduino.inf" + """"
Dim processInfo As New ProcessStartInfo("CMD", "/C InfDefaultInstall.exe " + infPath)
processInfo.WindowStyle = ProcessWindowStyle.Hidden
Dim p As System.Diagnostics.Process = Process.Start(processInfo)
p.WaitForExit()
End Sub
Private Sub InstallDriverInstallHinfSection()
Dim infPath As String = """" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\arduino.inf" + """"
Dim process As New Process()
process.StartInfo.FileName = "cmd.exe"
'fails with and without this
process.StartInfo.UseShellExecute = True
process.StartInfo.Verb = "runas"
process.StartInfo.Arguments = "/C RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection DefaultInstall 132 " + infPath
'MsgBox(process.StartInfo.Arguments, MsgBoxStyle.OkOnly, "InstallHinfSection")
process.Start()
process.WaitForExit()
End Sub

In .net vb is there a way to know if Yes or No was chosen in a SHELL command when User Account control appears

I am using .net vb ; I usually shell a program to run an update of my software; the problem I am facing is I don't know whether the user has chosen Yes or No when prompted to run the program in an alleviated mode i.e When the User Account control box appears.
How can I know whether Yes or No was chosen after the shell command was issued. .
This is my current code - I use runas for a higher windows then XP
Public Shared Sub UpdateElevated()
Try
'Shell(C_Drive & "\POS-UPD\pos-upd.exe", AppWinStyle.NormalFocus, False)
Dim osInfo As OperatingSystem = Environment.OSVersion
Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
startInfo.UseShellExecute = True
startInfo.WorkingDirectory = C_Drive & "\POS-UPD"
startInfo.FileName = C_Drive & "\POS-UPD\pos-upd.exe"
If osInfo.Version.Major >= 6 Then startInfo.Verb = "runas"
startInfo.WindowStyle = ProcessWindowStyle.Normal
Process.Start(startInfo)
Catch ex As Exception
End Try
End Sub

Running CMD command on windows service

I have created a windows service that requires executing an EXE file with the CMD process. I have used the following code:
Str = "C:\PCounter\Staff\account.exe CHARGE " & Name & " " & Amount & " TO" & Id
Dim procStartInfo As New System.Diagnostics.ProcessStartInfo(Str)
procStartInfo.RedirectStandardOutput = True
procStartInfo.UseShellExecute = False
procStartInfo.CreateNoWindow = True
Dim proc As New System.Diagnostics.Process
proc.StartInfo = procStartInfo
proc.Start()
proc.Dispose()
However the above code will return
system cannot find the file specified
I have tried same code on the Windows form, and its works fine. To make sure the path is correct I have added a text file in the same location as EXE file, and load the content of the text file in the service. It works fine.
I can't think of anything else; I really would appreciate it if you can help me on this.
ProcessStartInfo has two properties. One for the executable to run, and the other for the arguments to pass to the executable. The symantics for the Arguments property are the exact same as the command line.
You can not include the arguments in the same property as the executable. They must be separated.
Create service:
sc create Vm-Symantec04 binPath= "\"C:\App32\VMware\VMware Workstation\vmrun.exe\" -T ws start \"D:\VM\Sym04\Sym04.vmx\" nogui" depend= "VMAuthdService/VMnetDHCP/VMUSBArbService/VMware NAT Service" start= auto
Delete service:
sc delete Vm-Symantec04

Running powershell scripts from within a .NET windows app

I'm needing to run scripts from within a vb.net windows app.
I've got the scripts running in the background fine;
Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
MyRunSpace.Open()
Using MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript("import-module -name " & moduleName &
vbCrLf &
"(get-module -name " & moduleName & ").version")
Dim results = MyPipeline.Invoke()
'Do something with the results
End Using
MyRunSpace.Close()
End Using
However, i now need to be able to have the powershell run (not in the background) eg. When prompts occur;
Set-ExecutionPolicy unrestricted
I'm currently looking into the Microsoft.PowerShell.ConsoleHost namespace to see if i can use something like;
Dim config = RunspaceConfiguration.Create
ConsoleShell.Start(config, "Windows PowerShell", "", New String() {""})
Can anyone advise me please???
EDIT: I've fudged it a bit with this;
Public Function RunPowershellViaShell(ByVal scriptText As String) As Integer
Dim execProcess As New System.Diagnostics.Process
Dim psScriptTextArg = "-NoExit -Command ""& get-module -list"""
'Dim psScriptTextArg = "-NoExit -Command ""& set-executionPolicy unrestricted"""
'Dim psScriptTextArg = ""-NoExit -Command """ & scriptText & """"
execProcess.StartInfo.WorkingDirectory = Environment.SystemDirectory & "\WindowsPowershell\v1.0\"
execProcess.StartInfo.FileName = "powershell.exe"
execProcess.StartInfo.Arguments = psScriptTextArg
execProcess.StartInfo.UseShellExecute = True
Return execProcess.Start
End Function
But there's gotta be a better way??
There is a distinction between the PowerShell engine and its host. What you're wanting is to run the engine within your application but then fire up a separate host (which also is hosting the PowerShell engine) to handle prompts. You might want to look into modifying your application to act as a host itself. You could then react to prompts (read-host) and pop dialog boxes or whatever. Take a look at this relevant PowerShell namespace. Also check out this blog post on creating a simple PSHost.