Jump to content

Gui for command line help!


Recommended Posts

Yay, that works, thank you!

So now I just need to learn the text box, take text box input as add it as an argument.

I'll need to continue this in a VM because messing around with slmgr on a live system is dangerous, no?

Link to comment
Share on other sites


Oh and a question about Process.Start

Assuming I'll be running my app from a dvd, does it assume files in the directory it was run from if they aren't in a system path? IE I wouldn't have to specify which folder Keyfinder.exe was on the dvd if it's in the same folder as the executable for this app?

Link to comment
Share on other sites

And another question:

How difficult would it be relative to what we've done so far, to require the correct Windows product key format in the textbox? 5alphanumeric > a dash > 5 alphanumeric etc

I'm guessing MUCH more difficult?

Link to comment
Share on other sites

How difficult would it be relative to what we've done so far, to require the correct Windows product key format in the textbox? 5alphanumeric > a dash > 5 alphanumeric etc

Checking formatting is normally done using regular expressions.

Otherwise you could check that the length of the typed text is just enough chars, then making sure that each character (one by one) is either alphanumeric or a dash depending on their position but that's not much easier... Once the app is working then yes, you should add that kind of validation. I'll help you with it too (it's more complex indeed).

Link to comment
Share on other sites

Ok, I've got a strong rudimentary grasp of what we've done so far, I owe you a bunch.

I've added the textbox. Now I am working on using the input. Here's code so far:

Imports System.Diagnostics

Public Class Form1

Private Sub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click
Process.Start("slmgr.vbs")
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

What is the correct format for adding arguments, and how to use TextBox.Text?

Also I notice it defaulted to TextBox1.TextChanged, would I need to used .TextChanged (or rename all instances of TextChanged to Text)?

Link to comment
Share on other sites

This part:

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

isn't needed right now (although it doesn't do any harm just being there either).

Try this (type something in the textbox like dir or ping 127.0.0.1 before clicking the button):


Imports System.Diagnostics

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start("cmd.exe", "/K " & TextBox1.Text)
End Sub
End Class

Link to comment
Share on other sites

AND another question! (Feel free to climb up a tower with a high powered rifle any time I ask too many questions!)

If the entire text string would need to be entered enclosed in parenthesis in the command prompt (IE directories with long filenames), do they need parenthetical enclosure when run from a program like this? And if so, how would I go about doing that?

Link to comment
Share on other sites

So.. what I want is something like this:

Process.Start("slmgr.vbs", "-ipk " & TextBox1.Text)

?

That works just fine :)

Do you know if it REQUIRES dashes or if the 25 alphanumeric alone can be entered? And are they case insensitive?

As far as I know the dashes are required, and it's case insensitive.

Edit:

As for the regular expression (to validate the format of the typed text), you will have to add "Imports System.Text.RegularExpressions" at the top where the other Imports is/are.

A simple* regular expression to handle this would be: ^([A-Za-z0-9]{5}-){4}([A-Za-z0-9]{5})$

^ <- matches the beginning of the text

$ <- matches the end of the text

[A-Z] would match any single character between A and Z but we've added more "allowed ranges":

[A-Za-z0-9] will match any single character between A and Z *or* between a and z *or between 0 and 9 -- basically any digit of the serial

{5} means it will have to be repeated 5 times so:

[A-Za-z0-9]{5} will match a group of 5 characters, each being in the allowed ranges

- <- matches the dash in the key, so:

[A-Za-z0-9]{5}- matches a 5 allowed characters followed by a dash

() are used to create a group, so

([A-Za-z0-9]{5}-) will match a group of 5 allowed characters followed by a dash

([A-Za-z0-9]{5}-){4} will match a set of 4 groups of 5 allowed characters each followed by a dash

([A-Za-z0-9]{5}-){4}([A-Za-z0-9]{5}) will match 4 groups of 5 chars with their dash and a 5th group of 5 chars (no dash)

^([A-Za-z0-9]{5}-){4}([A-Za-z0-9]{5})$ will match the same thing, but only if nothing else is typed before or after that.

so the validator works like this:

Regex.IsMatch(TextBox1.Text, "^([A-Za-z0-9]{5}-){4}([A-Za-z0-9]{5})$")

This will return true if the text matches the regular expression i.e. if it's in the right format, otherwise it will return false.

So you would use a If statement, that will run the slgmr command line if the Regex returns true (proper key), otherwise popup an error message using a MessageBox. The result would like this:


Imports System.Diagnostics
Imports System.Text.RegularExpressions

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Regex.IsMatch(TextBox1.Text, "^([A-Za-z0-9]{5}-){4}([A-Za-z0-9]{5})$") Then
Process.Start("slmgr.vbs", "-ipk " & TextBox1.Text)
Else
MessageBox.Show("Key format is invalid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
End Class

I know that it might be quite a bit to grasp at once. Don't focus too much on the Regex but rather the logic and program flow instead (i.e. the if/then/else)

* By simple I mean that it fails to account for some alphanumeric characters that aren't allowed in CD keys (there's only 24 allowed characters out of the 36 letters & numbers), not that it's ridiculously easy to understand the first time you get into it.

Link to comment
Share on other sites

Awesome, this is whizzing right along!

So now I'm ready for the more advanced parts I think.

So I want Button0 to clear the window, and have the textbox, "Change Key" and "Cancel" buttons to appear. The text "Change Customer Product Key" should be displayed somewhere in the window.

Link to comment
Share on other sites

And urgh, I thought of two more questions :/

"slmgr.vbs" This is available in ALL versions of Vista/7, or is it a stripped out of some versions?

Will UAC interfere with this program's functions? And if so, is there a way to flag this program to require Admin rights to run?

Link to comment
Share on other sites

So I want Button0 to clear the window, and have the textbox, "Change Key" and "Cancel" buttons to appear

You mean, popup another window (form) with these on it? I'm not totally sure I followed.

And urgh, I thought of two more questions :/

There's an edit button at the bottom of your posts ;)

"slmgr.vbs" This is available in ALL versions of Vista/7, or is it a stripped out of some versions?

As far as I know, this is included in all editions of both.

Will UAC interfere with this program's functions? And if so, is there a way to flag this program to require Admin rights to run?

This program doesn't need to run elevated but slmgr does. If you don't run slmgr.vbs elevated it will just popup an error message. There is a way to make sure you run elevated but it's not perfect... Look over here to see how you check if you're running elevated (you check using .IsInRole(WindowsBuiltInRole.Administrator) on WindowsPrincipal). Then if you determine you're not running elevated, you basically make your app run itself but in a special way (you create a ProcessStartInfo and set its verb to "runas", which will then popup the UAC window automatically).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...