我在vb 2013中解析数据的知识非常有限,希望有更多经验的人可以提供建议: 概述 我想在我的控制台应用程序中添加三个命令行开关和值,然后将它们解析为变量,以便在我的程序中继续使用。命令行选项有望如下:
my.exe / d dateserial / p plaintext或
my.exe / d dateserial / c cyphertext这些选项取决于程序是加密还是取消加密任何给定的字符串。我还想加入一些基本的错误处理,比如if / c / p开关存在于同一命令行中,停止程序并显示用法。 我的项目目前看起来像这样:
Dim dtext As String = 字符串 .Empty ' 日期序列字符串 Dim ptext As String = 字符串 .Empty ' 纯文本字符串 Dim ctext As String = 字符串 .Empty ' cypher text string Dim clArgs() As String = Environment.GetCommandLineArgs() 如果 clArgs.Count()= 5 然后 对于 index 作为 整数 = 1 要 3 步骤 1 如果 clArgs(index)= d /; 然后 dtext = clArgs(索引+ 1 ) MsgBox(dtext)' 确保变量填充正确。 其他 Console.WriteLine( 用法:my.exe / d dateserial / p plaintext或my.exe / d dateserial / c cyphertext) 结束 如果 下一步 结束 如果代码片段仅在上方部分完成任务,所以提前感谢任何可以建议更完整和更强大的解决方案的人。
解决方案看看Regex.Split(): b $ b msdn.microsoft/en-us/librar y / system.text.regularexpressions.regex.split%28v = vs.110%29.aspx [ ^ ] 我认为这就是你需要的。 您可以执行以下操作: Dim yourArgs() as String = Regex.Split(clArgs , /) 对于 每个 arg as String yourArgs if arg.Contains( c)然后 ' 此处的代码 结束 如果 下一步
这只是一个如何使用正则表达式的快速而肮脏的例子.split()和.Contains()。有更优雅的方法,你应该看看String.Substring()和String.Replace()。 - Pete
请参阅我的文章,以及我参考和推荐的另一篇文章:基于枚举的命令行实用程序 [ ^ ]。 我提供了非常易于使用的库。现在,以防万一:请不要告诉我这是C#,而不是VB.NET。首先,在.NET中,这绝对不重要;与VB.NET相比,C#总是更好,因为它是标准化的。此外,您可以自动将任何程序集转换为VB.NET,质量非常好(特别是使用ILSpy)。谢谢你的想法,他们非常感谢。这是一个vb应用程序。然而,在进一步搜索和调整参数后,我使用以下代码实现了所需的结果。我还想整合一些基本的错误处理,因为我的控制台程序只有5个命令行参数并且需要修复这些字段,我想出了这个原始但有效的解决方案: Dim dtext As String = 字符串 .Empty Dim ptext 作为 字符串 = 字符串 .Empty Dim ctext 作为 字符串 = 字符串 .Empty Dim clArgs() As String = Environment.GetCommandLineArgs() 如果 clArgs.Count()< ;> 5 然后 Console.WriteLine( 正确用法:/ p plaintext / d dateserial或) Console.WriteLine( 正确用法:/ c cyphertext / d dateserial) 结束 如果 如果 不(clArgs.Contains( / p)或 clArgs.Contains( / c))和(clArgs.Contains( / d)) 然后 退出 Sub 结束 如果 如果 clArgs.Contains( / p)和 clArgs.Contains( / d)然后 对于 index 作为 整数 = 1 3 步骤 2 如果 clArgs(index)= / p 然后 ptext = clArgs(index + 1 ) dtext = clArgs(index + 3 ) Console.WriteLine(ptext) Console.WriteLine(dtext) 结束 如果 下一步 结束 如果 如果 clArgs.Contains( / c)和 clArgs.Contains( / d)然后 对于 index 作为 整数 = 1 要 3 步骤 2 如果 clArgs(index)= / c 然后 ctext = clArgs(index + 1 ) dtext = clArgs(索引+ 3 ) Console.WriteLine(ctext) Console.WriteLine (dtext) 结束 如果 下一步 结束 如果
Hi, I have limited very limited knowledge on parsing data in vb 2013 and hope someone with more experience can advise: Overview I would like to add three command line switches and values to my console application and then parse them into variables, for onward use within my program. The command line options would hopefully look like this:
my.exe /d dateserial /p plaintextor
my.exe /d dateserial /c cyphertextThese options depend on whether the program was encrypting or de-crypting any given string. I would also like in incorporate some basic error handling, like if /c /p switches exist on the same command line, stop the program and display usage. My project currently looks like this:
Dim dtext As String = String.Empty 'date serial string Dim ptext As String = String.Empty 'plain text string Dim ctext As String = String.Empty 'cypher text string Dim clArgs() As String = Environment.GetCommandLineArgs() If clArgs.Count() = 5 Then For index As Integer = 1 To 3 Step 1 If clArgs(index) = "d/"; Then dtext = clArgs(index + 1) MsgBox(dtext) ' To ensure variables get populated correctly. Else Console.WriteLine("Usage: my.exe /d dateserial /p plaintext or my.exe /d dateserial /c cyphertext") End If Next End IfThe code snip-it above only partly completes the task, so thanks in advance to anyone that can advise on a more complete and robust solution.
解决方案 Have a look at Regex.Split(): msdn.microsoft/en-us/library/system.text.regularexpressions.regex.split%28v=vs.110%29.aspx[^] I think that's what you'll need. You can do stuff like: Dim yourArgs() as String = Regex.Split(clArgs, "/") For Each arg as String in yourArgs if arg.Contains("c ") Then ' Your code here End If NextThis is just a quick and dirty example of how to use regex.split() and .Contains(). There are more elegant ways of doing this, and you should also have a look at String.Substring() and String.Replace(). - Pete
Please see my article, and also another article I reference and recommend in it: Enumeration-based Command Line Utility[^]. I provide extremely easy-to-use library. Now, just in case: please don't tell me "this is C#, not VB.NET". First, in .NET, this is absolutely not important; C# is always better because it is standardized, in contrast to VB.NET. Besides, you can automatically translate any assembly to VB.NET with very good quality (use ILSpy, in particular).更多推荐
Vb.net控制台应用程序解析命令行参数
发布评论