How to run Command Prompt commands from VB.NET? - asp.net

I have an application which build in vb.net.I never learn VB.I have worked on C#.net.when I run it's setup and execute it,I able to see command prompt.But when I using this application for scheduling task/creating task and scheduling, unable to see command prompt.
I am using following code that create process.
Dim Cw As New ProcessStartInfo(name, name & " " & name)
Cw.WindowStyle = ProcessWindowStyle.Maximized
Process.Start(Cw)
Is there is any other way to do this?Without making a process can I execute shell program?I am working on windows8 opeating system

You can use a simply Shell command, so:
Shell(path_to_your_exe, AppWinStyle.MaximizedFocus, true, 1000)
Be careful with path, if you have blanks escape they so
Shell(""d:\folder blank\my.exe"")
MSDN: https://msdn.microsoft.com/es-es/library/microsoft.visualbasic.interaction.shell(v=vs.110).aspx

If I understood the question, you might be using a Windows Form Application and you want to see the command line info of your external application.
In that case, you can use this code:
Sub Main()
Dim ProcessStartInfo = BuildProcessStartInfo("E:\path\to\your\exeFile.exe", "")
Dim proc = Process.Start(ProcessStartInfo)
End Sub
Public Function BuildProcessStartInfo(exeFilePath As String, arguments As String) As ProcessStartInfo
Dim startInfo = New ProcessStartInfo
startInfo.FileName = exeFilePath
startInfo.Arguments = arguments
startInfo.CreateNoWindow = False
startInfo.UseShellExecute = True
startInfo.WindowStyle = ProcessWindowStyle.Maximized
Return startInfo
End Function

Related

Schedule R scripts without using Task Scheduler

I have RStudio installed on my Windows 7 machine however I am unable to access Windows Task Scheduler to automate some R scripts. Are there any alternatives in this situation? I have explored taskscheduleR however found out that this is just a wrapper of Task Scheduler. Open to ideas using VB scripts - basically any hack will do.
I would suggest VBA for your task and you need to learn how to use Application.OnTime;
You need other subs that triggers and stops this one as you desire; first one can start the schedule.
Public stop_running As Boolean
Public sTime As String
Sub Start(input As String)
stop_running = True 'This controls schedule in Application.Ontime
sTime = input
Application.OnTime TimeValue(sTime), "Rscript", Schedule:=stop_running
End Sub
Whenever you want to stop running this macro, the you run this one;
Sub Finish()
stop_running = False 'Set the schedule to false to stop the macro
Application.OnTime TimeValue(sTime), "Rscript", Schedule:=stop_running
End Sub
Then this is the sub that runs your r-script:
Sub Rscript()
'runs R script through cmd
If Not stop_running Exit Sub
Dim shell As Object
Set shell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim style As Integer: style = 1
Dim errorCode As Integer
Dim path As String
path = "RScript C:\R\myscript.R"
errorCode = shell.Run(path, style, waitTillComplete)
End Sub
You can call the macro Start from immediate window like this:
Call Start(TimeValue("14:00:00 pm"))
And your macro will run at 2 p.m. Everyday.

Run R script through access vba

I'm trying to run an R Script from my access DB so it will process some data before it is imported, however i'm having trouble as it fails at the errrorCode on the last line.The scrip is:
Private Sub cmdRcode_Click()
Dim shell As Object
Set shell = VBA.CreateObject("WScript.Shell")
Dim waitTillComplete As Boolean: waitTillComplete = True
Dim style As Integer: style = 2
Dim errorCode As Integer
Dim path As String
path = "C:\Users\Liam\Dropbox (BGD_058)\BGD_058\19_BFI_data\Analysis\r_scripts\BFI_DAT_1.R"
errorCode = shell.Run(path, style, waitTillComplete)
End Sub
EDIT:
Upon advice from the comments i've changed my file path, avoid spaces and substituted the errorCode as follows:
path = "Rscript C:\Users\Documents\BFI_Field_Forms\R\BFI_DAT_1.R"
errorCode = shell.Run("C:\Users\R\BFI_DAT‌​_1.R", 1, True)
I then updated the environment path to Advanced System Settings/Environment Variable and created a new variable called "PATH" with the line: C:\Program Files\R\R-3.2.2\bin\x64;
The result is that it opens my R file, however it does not run the R script. The priority is for it to run the script (so the data is processed), and if possible, the R file would not be visible to the user.
Any advice appreciated.

Start an Instance of Autodesk Inventor

Am Using Inventor api for customizing inventor documents.Here I use vb.net code for start an instance of the Inventor .my code is
inventorApp = CreateObject("Inventor.Application", "")
inventorApp.Visible = True
it is ok and working fine .but when we open the visual studio run as administrator then the createobject having some error.Any one know any other way to start an instance of Inventor?
Try using the marshal method instead.
Dim m_inventorApp As Inventor.Application
Try ' Try to use active inventor instance
Try
m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")
m_inventorApp.SilentOperation = True
Catch ' If not active, create a new instance of Inventor
Dim inventorAppType As Type = System.Type.GetTypeFromProgID("Inventor.Application")
m_inventorApp = System.Activator.CreateInstance(inventorAppType)
' Must set visible explicitly
m_inventorApp.Visible = True
m_inventorApp.SilentOperation = True
End Try
Catch
'Cant get or create an instance of inventor.
End Try
Private Sub Open_Button_Click()
ThisApplication.SilentOperation = True 'Suppresses the resolve links dialog
Dim myPath As String
myPath = FileName.Text 'Gets the string, FileName, from module 1
Dim Shell As Object
Set Shell = CreateObject("Shell.Application")
Shell.Open (myPath) 'Opens selected file
Resolve_and_Open.Hide 'Hides module
CompareStrings
End Sub
This is to open a sad assembly that needs to resolve links. I'm not sure if this will get around that error, but try using this:
ThisApplication.SilentOperation = True
Either that, or creating a shell and then opening it that way instead of directly.

excuting cmd prompt through asp.net app, with more than one command line

I'm looking to run a command prompt with a "dir" command in my web app and read the response...which I've got working but I need to to do another command first i.e. I need to use a CD to get to the proper directory.
I can get a DIR response and write it to the screen by basically using this link :
http://weblogs.asp.net/guystarbuck/archive/2008/02/06/invoking-cmd-exe-from-net.aspx
or here is the code:
If Not IsPostBack Then
Dim _info As New ProcessStartInfo("cmd", "/C " & "DIR")
' The following commands are needed to redirect the
' standard output. This means that it will be redirected
' to the Process.StandardOutput StreamReader.
_info.RedirectStandardOutput = True
' Set UseShellExecute to false. This tells the process to run
' as a child of the invoking program, instead of on its own.
' This allows us to intercept and redirect the standard output.
_info.UseShellExecute = False
' Set CreateNoWindow to true, to supress the creation of
' a new window
_info.CreateNoWindow = True
' Create a process, assign its ProcessStartInfo and start it
Dim _p As New Process()
_p.StartInfo = _info
_p.Start()
' Capture the results in a string
Dim _processResults As String = _p.StandardOutput.ReadToEnd()
' Close the process to release system resources
_p.Close()
' Return the output stream to the caller
Response.Write(_processResults)
End If
The trouble it's in the wrong directory...how can I do a CD otherdir first?
Anyone know?
Thanks,
You could try with this command concatenation character
Dim _info As New ProcessStartInfo("cmd", "/C C: & CD C:\TEMP & DIR")
Why so complicated? You can use this one-liner:
string[] filePaths = Directory.GetFiles(#"c:\MyDir\", "*.bmp");
to get an array of all files (in this case, all .bmp-files) in the given directory. The extension parameter is optional.

Getting "Operation is not valid due to the current state of the object" error for Exchange commands

I have an ASP.NET 3.5 application running Powershell scripts to add/update Exchange mailboxes. Most of the Powershell commands work great, but two of them so far have caused an error: get-calendarprocessing and set-calendarprocessing, which get or set users assigned to schedule a Room. The error is System.Management.Automation.CmdletInvocationException: Operation is not valid due to the current state of the object. When I run the commands from the Exchange Management Shell, they work fine. We have patched and rebooted the Exchange server.
Command example:
get-calendarprocessing Room.1 | select -expand bookinpolicy (or similar variations)
Private Function RunScript(ByVal ScriptFileName As String, ByVal Arguments() As String) As Collection(Of PSObject)
Dim sb As New StringBuilder
Dim strScript As String
' create Powershell runspace and open
If psRunspace Is Nothing Then
psRunspace = InitializeRunspace()
End If
'Grab Powershell script from text (.ps1) file
strScript = File.ReadAllText(ScriptFileName)
'inject the arguments into the script
strScript = InsertArguments(strScript, Arguments)
Logging.LogMessage(strScript)
'Open the runspace and create a pipeline if it's not already open
If psRunspace.RunspaceStateInfo.State = RunspaceState.BeforeOpen Then
psRunspace.Open()
End If
Dim MyPipeline As Pipeline = psRunspace.CreatePipeline()
MyPipeline.Commands.AddScript(strScript)
Try
Dim psResults As Collection(Of PSObject)
psResults = MyPipeline.Invoke() 'ERRORS HERE
Return psResults
Catch ex As Exception
Logging.LogMessage(ex.Message)
Throw ex
End Try
End Function

Resources