November 19, 2007 at 11:14 am
· Filed under Visual Basic .NET
Lately I’ve been struggling to find a built-in function in vb.net that, in a console application, would return a specific command line argument. However – I did’nt find it.
Instead, I wrote this piece of code to do the function, that can be used when calling the .exe like this: "c:\programname.exe /file:c:\folder\filename.txt"
”’ <summary>
”’ A function that returns a specific Command Line Argument from format c:\commandname.exe /arg:value
”’ </summary>
”’ <param name="arg">The name of the argument to return</param>
”’ <param name="oArgs">An arraylist of arguments</param>
”’ <returns>String</returns>
”’ <remarks></remarks>
Private Function ReturnSpecificCommandLineArg(ByVal arg As String, ByVal oArgs As ArrayList) As String
Dim _ReturnString As String = String.Empty
Try
With oArgs
‘We don’t really need to sort – but hey!
.Sort()
‘Looping throug the array to find the arguments
For i As Integer = 0 To .Count – 1
If InStr(.Item(i).ToString, arg) > 0 Then
‘Getting the specific value
_ReturnString = .Item(i).ToString.Substring(InStr(.Item(i).ToString, ":"))
‘We’ve found the value – so we need to exit our for-loop
Exit For
Else
‘Returning an empty string if we did’nt find what we were looking for
_ReturnString = String.Empty
End If
Next
End With
Return _ReturnString
Catch ex As Exception
‘Handling errors
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Permalink
August 30, 2007 at 2:19 pm
· Filed under Visual Basic .NET
Small function to delete a user from Active Directory.
”’ <summary>
”’ A function that deletes a user from Active Directory
”’ </summary>
”’ <param name=”DirEntry”>The Directory Entry that specifies the OU that holds the user</param>
”’ <param name=”sCommonName”>The Common Name (CN) of the user to delete</param>
”’ <returns>true/false</returns>
”’ <remarks></remarks>
Private Function AdUserDelete(ByVal DirEntry As DirectoryEntry, ByVal sCommonName As String) As Boolean
‘Returning false if something goes wrong
AdUserDelete = False
Try
‘Finding the user
Dim _User As DirectoryEntry = DirEntry.Children.Find(“CN=” & sCommonName, “User”)
‘Removing the user
DirEntry.Children.Remove(_User)
‘Committing the changes
DirEntry.CommitChanges()
‘Closing
DirEntry.Close()
‘Went well – returning true
AdUserDelete = True
Catch ex As Exception
‘Throwing an error
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Permalink
August 30, 2007 at 1:37 pm
· Filed under Visual Basic .NET
This small function can be used to set the password for a specific user in the AD.
”’ <summary>
”’ Function that sets the password for a specific user
”’ </summary>
”’ <param name=”DirEntry”>The DirectoryEntry to use eq the user</param>
”’ <param name=”sPassword”>The password</param>
”’ <returns>true/false</returns>
”’ <remarks></remarks>
Private Function AdUserSetPassword(ByVal DirEntry As DirectoryEntry, ByVal sPassword As String) As Boolean
‘Return false if something goes wrong
AdUserSetPassword = False
Try
‘Creating an object-”array” to store the password
Dim _Password As Object() = New Object() {sPassword}
‘Calling a method
DirEntry.Invoke(“SetPassword”, _Password)
‘Returning true
AdUserSetPassword = True
Catch ex As Exception
‘Throwing exception
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Permalink
August 30, 2007 at 8:24 am
· Filed under Visual Basic .NET
To update a user property in your Active Directory (e.g. telephone, common name, description), you can use this function. The DirectoryEntry should point to the specific user that you wish to update.
”’ <summary>
”’ A function that set the property of an object in Active Directory
”’ </summary>
”’ <param name=”DirEntry”>The DirectoryEntry to use eq the user</param>
”’ <param name=”PropertyName”>The property to set</param>
”’ <param name=”PropertyValue”>The value to set to the property</param>
”’ <returns>true/false</returns>
”’ <remarks></remarks>
Private Function AdUserSetProperty(ByVal DirEntry As DirectoryEntry, ByVal PropertyName As String, ByVal PropertyValue As String) As Boolean
AdUserSetProperty = False
‘Specifying Administrator Account
DirEntry.Username = My.Settings.AdAdminUserName
DirEntry.Password = My.Settings.AdAdminPassword
Try
‘If the property has a value
If Not PropertyValue Is Nothing Then
‘Does the property exist
If DirEntry.Properties.Contains(PropertyName) Then
‘Setting the new value
DirEntry.Properties(PropertyName)(0) = PropertyValue
Else
‘Creating the property if it does not exist
DirEntry.Properties(PropertyName).Add(PropertyValue)
End If
‘Applying the changes
DirEntry.CommitChanges()
‘Closing
DirEntry.Close()
End If
‘Returning
AdUserSetProperty = True
Catch ex As Exception
‘Returning
AdUserSetProperty = False
‘Throwing an error
Throw New Exception(ex.Message, ex.InnerException)
End Try
End Function
Permalink
July 13, 2007 at 9:36 am
· Filed under Visual Basic .NET
I’m always struggling to finding built-in functions in vb.net, that does these small things: roudning up, subtracting dates, formatting dates, math-functions etc. But, now I’ve found this great website:
http://msconline.maconstate.edu/tutorials/VBNET/
It might not have all the functions – but it has the ones I need the most. Anyone has other good links?
Technorati Tags:
Functions Permalink
July 3, 2007 at 9:22 pm
· Filed under Visual Basic .NET
I have done it before – and I did again today… Used about an hour trying to figure out, why a service for Windows that I created using Visual Studio 2005 did’nt work at all. It started great, restartet – all was perfect – but it did’nt tick.
Until I finally found out the hard way, that you have to add a System.Timers.Timer timer instead of the Windows.Forms.Timer. I actually followed a guide somewhere on the Internet saying that I could just drag-n-drop the Timer from Components in the Service Project. But, it did’nt really help.
But what did help was adding this as the first line in the class:
Friend WithEvents tmrCheckShows As New System.Timers.Timer
And now the service is ticking – or elapsing.
Permalink
June 17, 2007 at 11:29 am
· Filed under Visual Basic .NET, asp.net
In this short guide, I’ll show how to use a dedicated SQL Server for the Membership and Roles part of ASP.NET. Normally the membership and roles uses the local SQL Express Server and a aspnetdb in the local app_data directory of the webserver. But for many reasons (one of them central backup) I have always wanted to be able to use my existing SQL Server (in my case SQL Server 2005).
1) Create a new database on your SQL Server to hold the membership and roles.
2) In my case I’ll create a new ASP.NET website to use for this demo. The website will be called "demo_membership".
3) Populate the database you created with the membership tables. For this, you’ll use the program aspnet_regsql located in: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 – doubleclick to launch.



If you expand the database – you should see the membership/roles tables:

The database is now reade for use.
Read the rest of this entry »
Permalink
January 25, 2007 at 4:01 pm
· Filed under Visual Basic .NET
And, if you wan’t to change the text of a cell – then you’re able to use:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(2).Text = “This is my text”
End If
End Sub
Permalink
January 25, 2007 at 3:41 pm
· Filed under Visual Basic .NET
Hm… Actually took me some time to figure out how to change the headertext at runtime for a column in a gridview.
But after som searching I found:
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(1).Text = “Show me the money”
End If
End Sub
But, why can’t I just, when binding the gridview use: GridView1.Columns(1).HeaderText = “Show me the money”
Permalink
September 5, 2006 at 9:36 pm
· Filed under Visual Basic .NET
Although I know, that the majority of people use C# for creating plugins to Windows Live Writer – I’d still like to make a small guide into the basic concept of creating a plugin using VB.NET.
1) Create a new project in Visual Studio .NET
Select the template "Class Library" and type the name of your project in the Name field.
2) Delete the default class created.
3) Add a new class to your project.

4) Add a reference to the Windows Live Writer API.

- and a reference to System.Windows.Forms

5) Import the namespaces
| Imports WindowsLive.Writer.Api |
| Imports System.Windows.Forms |
7) Create a Folder in your project and name it Images:

Create a new PNG-image at the size of 20 by 18 pixels and save it in your "Images"-folder.
Add the file as a ressource
- Double-click "My Project"
- Click "Resources"
- Click "Add Resource" – and make sure you click "Add Existing File", and select the image saved in your Images-folder
- If you find that type system.drawing.bitmap is not defined – try adding and removing a form.
10) Click your image in the Images-folder and under Build Action select "Embedded Resource".
9) Add some code (Please note, that the GUID should be unique for your plugin – if you need a guid – then go to http://www.replymedia.net/guid):
| Imports WindowsLive.Writer.Api |
| Imports System.Windows.Forms |
|
| <WriterPluginAttribute("05D06AD8-694D-458B-9631-F8F19F88A9BA", _ |
| "Insert current date and time", _ |
| Description:="Inserts current date and time", _ |
| ImagePath:="DemoPlugin.png", _ |
| HasEditableOptions:=False, _ |
| PublisherUrl:="http://tech.pederborgpoulsen.dk"), _ |
| InsertableContentSourceAttribute("Current Date and Time")> _ |
| Public Class Plugin |
| Inherits ContentSource |
|
| 'Overrides the function CreateContent |
| Public Overrides Function CreateContent(ByVal dialogOwner As System.Windows.Forms.IWin32Window, ByRef newContent As String) As System.Windows.Forms.DialogResult |
|
| Try |
| 'Adding content to newContent - newContent is returned to Windows Live Writer |
| newContent = FormatDateTime(Now(), DateFormat.GeneralDate).ToString |
|
| 'Returns to caller |
| Return DialogResult.OK |
|
| Catch ex As Exception |
|
| 'Do some error-handling |
|
| End Try |
|
| End Function |
|
| End Class |
10) Build the project and copy the DLL-file to your Windows Live Write Plugin-directory.
11) Start or Restart Windows Live Writer – and notice that your plugin appears in the Insert-list:

12) And that clicking on it inserts the current date and time: 05-09-2006 21:32:33
… Happy Coding …
Permalink