您的位置:程序门 -> .net技术 -> c#



获得命令行参数简单问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


获得命令行参数简单问题[已结贴,结贴人:bejon]
发表于:2007-01-10 01:31:50 楼主
在程序里注册了一个扩展名,双击该类型文件时会运行关联的程序
以前在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

不知道这是为什么呢。。。
发表于:2007-01-10 10:51:231楼 得分:0
正确的方法应该是第一种
发表于:2007-01-10 10:52:122楼 得分:20
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
}
发表于:2007-01-10 11:54:013楼 得分:0
可目前两种办法都不能获得文件名啊
但是在命令行那里手工输入却又可以获得文件名。。。。

还有其他办法吗?
发表于:2007-01-10 12:02:354楼 得分:20
用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
'

 
发表于:2007-01-10 12:03:215楼 得分:0
c#
//   sample   for   the   environment.getcommandlineargs   method
using   system;

class   sample  
{
        public   static   void   main()  
        {
        console.writeline();
//     invoke   this   sample   with   an   arbitrary   set   of   command   line   arguments.
        string[]   arguments   =   environment.getcommandlineargs();
        console.writeline( "getcommandlineargs:   {0} ",   string.join( ",   ",   arguments));
        }
}
/*
this   example   produces   the   following   results:

c:\> getcommandlineargs   arbitrary   text

getcommandlineargs:   getcommandlineargs,   arbitrary,   text
*/

 
发表于:2007-01-10 12:18:036楼 得分:0
这是注册扩展名的方法,帮忙看下最后一行有错吗?
    public   sub   createexpname()
                dim   sysreg   as   registrykey
                dim   subreg   as   registrykey
                sysreg   =   registry.classesroot
                subreg   =   sysreg.createsubkey( ". "   +   strapp.expname)
                subreg.setvalue( " ",   strapp.expname,   registryvaluekind.string)  
                subreg   =   sysreg.createsubkey(strapp.expname)
                subreg.setvalue( " ",   "公司名 ",   registryvaluekind.string)

                dim   subreg2   as   registrykey   =   subreg.createsubkey( "defaulticon ")
                subreg2.setvalue( " ",   application.EXECutablepath   +   ",0 ")
                subreg   =   subreg.createsubkey( "shell ")
                subreg   =   subreg.createsubkey( "open ")
                subreg   =   subreg.createsubkey( "command ")
                subreg.setvalue( " ",   application.EXECutablepath   +   "   1% ",   registryvaluekind.string)
        end   sub  

或者帮忙看下整个过程有没有错?
发表于:2007-01-10 12:21:297楼 得分:0
我把那个   1%   换成   %1也不行
发表于:2007-01-10 12:55:378楼 得分:0
改成subreg.setvalue( " ",   " " " "   +   application.EXECutablepath   +   "   %1 " " ",   registryvaluekind.string)
或者subreg.setvalue( " ",   " " " "   +   application.EXECutablepath   +   " " "   %1 ",   registryvaluekind.string)
效果均一样,不能获得文件名  

估计是注册表那出问题了,可是出在哪呢?
发表于:2007-01-10 13:02:139楼 得分:0
你看看这个例子
system   file   association
http://www.codeproject.com/useritems/system_file_association.asp
发表于:2007-01-10 18:55:3510楼 得分:0
knight94(愚翁)用的是什么网络啊,我上国外的网络都很慢
我用adsl   2m那种。。。
发表于:2007-01-10 20:24:4511楼 得分:0
看了那个地址,做法和我基本一样,不过他是硬把简单的东西复杂化了,鬼佬很喜欢这么做的吗?
发表于:2007-01-11 10:53:0412楼 得分:5
学习。。
发表于:2007-01-11 11:00:0813楼 得分:5
看看注册表注册以后的结果呢,只要注册以后的结果是正确的,这个就没有问题。

另外,第一种方法上应该是[],而不是()。
发表于:2007-01-11 14:35:4014楼 得分:0
经过n次测试,都没办法找到原因。
然后把代码放出来,换一个扩展名,结果证明程序是正确的。。。
而我在原来的程序为什么n次测试都不能获得文件名呢?估计是因为在正确之前进行了n次正确测试,这些测试又产生了一些错误的信息残留在注册表里,影响了以后的测试

另外,这么折腾,至少让我多知道了3,4种获得运行时的命令参数的办法。总结,获得命令参数的办法超过5种.....(难以相信)

看鬼佬乱七八糟的程序有时真的很浪费时间,代码质量太低。
发表于:2007-01-11 14:37:0615楼 得分:0
对了,正确代码的最后一行应换成:
subreg.setvalue( " ",   application.EXECutablepath   +   "   %1 ",   registryvaluekind.string)

那个数字1在后面......


快速检索

最新资讯
热门点击