-
Apr 29th, 2014, 03:29 PM
#1
Thread Starter
Hyperactive Member
Query string in windows app
My asp.net app will take query string as parameters like below:
thhp://order.aspx?ordername=aaa&ordercity=london
Can windows app do the same way? like below:
order.exe aaa london
order.exe will take "aaa" and "london" as passing parameter and process.
-
Apr 29th, 2014, 04:55 PM
#2
Re: Query string in windows app
Hello,
You can pass ordername=aaa&ordercity=london to the executable then parse the string delimited by & then test to see what was given too you and if the needed information is there go forward, else decide what to do.
Code:
Dim arguments As String() = Environment.GetCommandLineArgs()
Dim ordername As String = ""
Dim ordercity As String = ""
If arguments.Count = 2 Then
Dim Temp = arguments(1).Split("&"c)
For Each c In Temp
If c.ToLower.Contains("ordername=") Then
ordername = c.ToLower.Replace("ordername=", "")
End If
If c.ToLower.Contains("ordercity=") Then
ordercity = c.ToLower.Replace("ordercity=", "")
End If
Next
If Not String.IsNullOrWhiteSpace(ordername) AndAlso Not String.IsNullOrWhiteSpace(ordercity) Then
Console.WriteLine("{0} {1}", ordername, ordercity)
End If
End If
To test this, under project properties, Debug tab enter under Start Options -> Command line arguments ordername=aaa&ordercity=london then in form load use the code above.
-
Apr 30th, 2014, 10:36 AM
#3
Thread Starter
Hyperactive Member
Re: Query string in windows app
I tried but got two errors said that
arguments.Count is not a member of "system.array"
'c' is not declared.
Last edited by aspfun; Apr 30th, 2014 at 10:41 AM.
-
Apr 30th, 2014, 11:28 AM
#4
Re: Query string in windows app
For one, the count of the comandlineargs will always be at least one (it includes the exe name), you should check vs 3
if you are getting c is not declared is because it is not, you must have Option Explicit on (as you should) then either declare c on the dim section or on the loop itself (For Each c As string).
the other error we should see how you typed the code. Because it looks like you did something like array.arguments.count
Last edited by kaliman79912; Apr 30th, 2014 at 11:36 AM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 30th, 2014, 11:34 AM
#5
Re: Query string in windows app
You can pass the arguments as you posted, without the placeholder, just the values separated by spaces
Code:
Dim arguments As String() = Environment.GetCommandLineArgs()
Dim ordername As String = ""
Dim ordercity As String = ""
If arguments.Count = 3 Then
ordername = arguments(1)
ordercity = arguments(2)
End If
This is kevin's code adapted to what I just said. I am not saying this is the correct way, its less explicit and prone to user mistakes, but it is simpler.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 30th, 2014, 11:37 AM
#6
Re: Query string in windows app
I just noticed that kevin was checking vs 2 not because of your two arguments, but because he is handling both in just one single string. My bad.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Apr 30th, 2014, 02:24 PM
#7
Re: Query string in windows app
Originally Posted by aspfun
I tried but got two errors said that
arguments.Count is not a member of "system.array"
'c' is not declared.
Revised
Code:
Dim arguments As String() = Environment.GetCommandLineArgs()
Dim ordername As String = ""
Dim ordercity As String = ""
If arguments.Count = 2 Then
Dim Temp = arguments(1).Split("&"c)
For Each c As String In Temp
If c.ToLower.Contains("ordername=") Then
ordername = c.ToLower.Replace("ordername=", "")
End If
If c.ToLower.Contains("ordercity=") Then
ordercity = c.ToLower.Replace("ordercity=", "")
End If
Next
If Not String.IsNullOrWhiteSpace(ordername) AndAlso Not String.IsNullOrWhiteSpace(ordercity) Then
Console.WriteLine("{0} {1}", ordername, ordercity)
End If
End If
I do a good deal of coding which requires Option Infer=On for LINQ/Lambda so I did not cast c as string. I always code with Option Strict On, Option Explicit On, Option Infer On. Now with that mentioned, I do not recommend you or anyone else using Option Infer On unless you have a reason, otherwise it can cause more problems then it's worth. Once you work with LINQ/Lamba which create Anonymous Types you will understand. So in my projects I have the options as just mentioned while some I will have Option Infer Off and Option Strict On in specific classes.
Example which requires Option Infer On
Code:
Dim Results =
(
From T In TemperaturesDict.GroupBy(
Function(f) 3 > 0)) _
.Select(Function(group) _
New With
{
.High = group.Max(Function(y) y.Value),
.Low = group.Min(Function(y) y.Value),
.Avgerage = group.Average(Function(y) y.Value)
}
).First
Console.WriteLine("Average: {0} High: {1} Low: {2}",
Results.Avgerage,
Results.High,
Results.Low
)
-
May 1st, 2014, 02:58 PM
#8
Re: Query string in windows app
Originally Posted by aspfun
My asp.net app will take query string as parameters like below:
thhp://order.aspx?ordername=aaa&ordercity=london
Can windows app do the same way? like below:
order.exe aaa london
order.exe will take "aaa" and "london" as passing parameter and process.
Yes, this has existed since long before the internet, it's called "Command Line Arguments", I suggest reading up on it & of course you're not the first to ask about it in the .Net age: http://www.vbdotnetforums.com/consol...arameters.html
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|