vb.net 控制台应用程序返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17421459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Console Application return value
提问by Arun
I am New in console application .I Need to pass two command line arguments to the console application from a web application and get the returned result from console app.
我是控制台应用程序的新手。我需要从 Web 应用程序向控制台应用程序传递两个命令行参数,并从控制台应用程序获取返回的结果。
here I tried in we
我在这里试过我们
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim proc = New Process() With { _
.StartInfo = New ProcessStartInfo() With { _
.FileName = "C:\Users\Arun\Documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe", _
.Arguments = TextBox1.Text & " " & TextBox2.Text, _
.UseShellExecute = False, _
.RedirectStandardOutput = True, _
.CreateNoWindow = True _
} _
}
proc.Start()
proc.WaitForExit()
Response.Write(proc.ExitCode.ToString())
End Sub
my console application code is
我的控制台应用程序代码是
Public Function Main(sArgs As String()) As Integer
Return sArgs(0)
End Function
but I can't get the returned value from console app.What is the problem anyone Help?
但我无法从控制台应用程序获取返回值。有什么问题有人帮忙吗?
回答by varocarbas
This is not the way in which the parameters are passed to a VB.NET console program (as you can see here).
这是不是其中的参数传递给VB.NET控制台程序(正如你所看到的方式在这里)。
An example:
一个例子:
Module Module1
Sub Main()
For Each arg As String In My.Application.CommandLineArgs
Console.WriteLine(arg)
Next
End Sub
End Module
If you generate a console-project EXE (app.exe) consisting exclusively in the code above and call it (from cmd) like this: [full_path]app 1 2, you would get 1 2printed in your screen.
如果您生成一个仅包含在上面的代码中的控制台项目 EXE (app.exe) 并像这样调用它(从 cmd):[full_path]app 1 2,您将被1 2打印在屏幕上。
Thus, all what you have to do is retrieving the arguments from My.Application.CommandLineArgs.
因此,您所要做的就是从My.Application.CommandLineArgs.
-------- UPDATE AFTER THE EXACT REQUIREMENTS HAVE BEEN EXPLAINED BETTER
-------- 在更好地解释确切要求后更新
Under .Argumentsyou have to put just the arguments you want to pass to the console application.
在.Arguments您必须将要传递给控制台应用程序的参数放在下面。
You can return more than just one integer to the calling program by relying on a simple temp file. For example:
依靠一个简单的临时文件,您可以向调用程序返回多个整数。例如:
CONSOLE PROGRAM:
控制台程序:
Dim writer As New System.IO.StreamWriter("temp")
writer.Write("anything")
writer.Close()
CALLING PROGRAM:
调用程序:
Dim reader As New System.IO.StreamReader("temp")
Dim line As String
Do
line = sr.ReadLine() 'reading anything passed from the console
Loop Until line Is Nothing
reader.Close()
Try
System.IO.File.Delete("temp")
Catch ex As Exception
End Try
回答by Matt Wilko
You can't natively return two separate arguments, you are limited A 32-bit signed integer
你不能本地返回两个单独的参数,你被限制了一个 32 位有符号整数
The only way I can think of doing this is if you have two numeric values that you can guarantee are less than 16 bits each then you could combine these into one 32bit value by bit shifting one of them.
我能想到的唯一方法是,如果您有两个数值,并且可以保证每个数值都小于 16 位,那么您可以通过对其中一个进行位移来将它们组合成一个 32 位值。
This code should get you started:
此代码应该可以帮助您入门:
Public Shared Function CombineValues(val1 As Int16, val2 As Int16) As Int32
Return val1 + (CInt(val2) << 16)
End Function
Public Shared Sub ExtractValues(code As Int32, ByRef val1 As Int16, ByRef val2 As Int16)
val2 = CShort(code >> 16)
val1 = CShort(code - (CInt(val2) << 16))
End Sub
Usage (console):
用法(控制台):
'in your console app combine two int16 values into one Int32 to use as the exit code
Dim exitCode As Int32 = CombineValues(100, 275)
Debug.WriteLine(exitCode) 'Output: 18022500
Usage (Calling code):
用法(调用代码):
'In the calling app split the exit code back into the original values
Dim arg1 As Int16
Dim arg2 As Int16
ExtractValues(exitCode, arg1, arg2)
Debug.WriteLine(arg1.ToString + "; " + arg2.ToString) 'Output: 100; 275

