日期:2014-05-20  浏览次数:20714 次

获得命令行参数简单问题
在程序里注册了一个扩展名,双击该类型文件时会运行关联的程序
以前在vb6时双击那种扩展名的文件,运行程序后使用command方法获得命令参数,可现在使用以下两种方法均不能获得命令参数:
'第一种办法
        Public   Shared   Sub   main(ByVal   strC()   As   String)
                Try
                        If   strC(0).Length   >   0   Then
                                msgbox(strc(0))
                        End   If
                Catch   ex   As   Exception
                        '出错不处理
                End   Try
                Application.Run(New   info)
        End   Sub
'第二种办法(在formload事件中)
Dim   str1   As   String   =   GetCommandLine
if   str1.lenght> 0   then     str1   =   str1.Substring(Application.ExecutablePath.Length   +   1).Trim
但是,如果是在开始菜单的运行框那输入
程序名   xxxx.abc
以上两种办法都可以获得xxxx.abc

不知道这是为什么呢。。。

------解决方案--------------------
sample code as follows:
[STAThread]
static void Main(string[] args)
{
if( args != null )
{
foreach( string arg in args )
MessageBox.Show( arg );
}
//run main form here
}
------解决方案--------------------
用Environment.GetCommandLineArgs 方法
如:

Visual Basic
' Sample for the Environment.GetCommandLineArgs method
Imports System

Class Sample
Public Shared Sub Main()
Console.WriteLine()
' Invoke this sample with an arbitrary set of command line arguments.
Dim arguments As [String]() = Environment.GetCommandLineArgs()
Console.WriteLine( "GetCommandLineArgs: {0} ", [String].Join( ", ", arguments))
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'C:\> GetCommandLineArgs ARBITRARY TEXT
'
'GetCommandLineArgs: GetCommandLineArgs, ARBITRARY, TEXT
'



------解决方案--------------------
学习。。
------解决方案--------------------
看看注册表注册以后的结果呢,只要注册以后的结果是正确的,这个就没有问题。

另外,第一种方法上应该是[],而不是()。