Windows Phone Developers

Wednesday, April 30, 2008

Get Free Disk Space in all drives using VB.Net

Get Available Disk Space using VB.Net


Sub GetSpaceInDrives()

'Dim oDinfo As DriveInfo

Dim oDrvs() As DriveInfo = DriveInfo.GetDrives

For Each Drv In oDrvs

If Drv.IsReady Then

MsgBox(Drv.Name & " " & Drv.AvailableFreeSpace.ToString)

End If

Next

End Sub

The statement

Dim oDrvs() As DriveInfo = DriveInfo.GetDrives

retrieves the drive names of all logical drives on a computer.

The IsReady property returns true if the drive is ready; false if the drive is not ready. If this check is not done then you will get System.IO.IOException was unhandled Message="The device is not ready. " exception.

AvailableFreeSpace property indicates the amount of free space available on the drive. Note that this number may be different from the TotalFreeSpace number because this property takes into account disk quotas

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Copying Files using .Net Functions

CopyFile Function in VB.Net


Imports System.IO

Function CopyFile(ByVal sSourceFile As String, ByVal sDestnFile As String)

Dim oFile As New FileInfo(sSourceFile)

If oFile.Exists Then

oFile.CopyTo(sDestnFile)

Else

MsgBox("File Does not exist")

End If

End Function

Use the FileInfo class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files.

Many of the FileInfo methods return other I/O types when you create or open files. You can use these other types to further manipulate a file. For more information, see specific FileInfo members such as Open, OpenRead, OpenText, CreateText, or Create.

If you are going to reuse an object several times, consider using the instance method of FileInfo instead of the corresponding static methods of the File class, because a security check will not always be necessary.

By default, full read/write access to new files is granted to all users.

The following table describes the enumerations that are used to customize the behavior of various FileInfo methods.

Enumeration

Description

FileAccess

Specifies read and write access to a file.

FileShare

Specifies the level of access permitted for a file that is already in use.

FileMode

Specifies whether the contents of an existing file are preserved or overwritten, and whether requests to create an existing file cause an exception.

NoteNote:

In members that accept a path as an input string, that path must be well-formed or an exception is raised. For example, if a path is fully qualified but begins with a space, the path is not trimmed in methods of the class. Therefore, the path is malformed and an exception is raised. Similarly, a path or a combination of paths cannot be fully qualified twice. For example, "c:\temp c:\windows" also raises an exception in most cases. Ensure that your paths are well-formed when using methods that accept a path string.





In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

· "c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

· "c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

· "MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

· "\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

It is always a good practice to check the existence of the file with Exists property before performing operations

If oFile.Exists Then

oFile.CopyTo(sDestnFile)

Exists will return true if the file exists; false if the file does not exist or if the file is a directory.


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Monday, April 28, 2008

.NET 3.5 Enhancements Training Kit Available for Download

Download .NET 3.5 Enhancements Training Kit


The .NET Framework 3.5 Enhancements Training Kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the .NET 3.5 Enhancement features including: ASP.NET MVC, ASP.NET Dynamic Data, ASP.NET AJAX History, ASP.NET Silverlight controls, ADO.NET Data Services and ADO.NET Entity Framework.

System Requirements

  • Supported Operating Systems: Windows Vista; Windows XP
Microsoft Windows Vista
Microsoft Visual Studio 2008
Microsoft SQL Server 2005 (Express recommended)
Microsoft Office Powerpoint 2007 or the PowerPoint Viewer 2007 - Required to view the presentations
Windows PowerShell 1.0 RTM

Top of page

Instructions

Download and execute the kit. The kit will uncompress to the selected folder and launch a HTML browser for the content.

Top of page

Additional Information

This kit was developed by the Developer and Platform Evangelism Group.

Download the Kit http://www.microsoft.com/downloads/details.aspx?FamilyID=355c80e9-fde0-4812-98b5-8a03f5874e96&displaylang=en#AdditionalInfo

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Sunday, April 27, 2008

Check Type of Variable in VB.Net

Identify Variable Type in VB.NET

Sub Check_Variable_Types()

Dim varByte As SByte

Dim varInt As Integer

Dim varString As String

Dim varExp As Exception

varString = "Temp"

varExp = New System.Exception("Sample Exception")

Dim varArr As Object() = {varByte, varInt, varString, varExp}

'The Object data type can point to data of any data type, including any object instance your application recognizes. Use Object when you do not know at compile time what data type the variable might point to.

'The default value of Object is Nothing (a null reference).

MsgBox(varArr.GetType.ToString())

For Each obj As Object In varArr

MsgBox(obj.GetType.IsValueType) 'Gets a value indicating whether the Type is a value type.

MsgBox(obj.GetType().ToString)

Next

End Sub

' Value types are those that are represented as sequences of bits; value types are not classes or interfaces. These are referred to as "structs" in some programming languages. Enums are a special case of value types.

'This property returns false for the ValueType class.

'This property returns true for enumerations, but not for the Enum type itself. For an example that demonstrates this behavior, see IsEnum.

'This property is read-only.


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Nullable Type in Visual Basic.Net

Storing Null Values in VB.Net Variables

Most of times we use variables to store the response from the user; might be a simple message box. In that case, we would be using a boolean variable - one that can store True or False. What if the User didn't answer the question? It would be taken as False by default. To overcome this you can use the variable as Nullable. Now the var can have three states - True , False and Null

Sub Nullable_Example()

' Nullable type will be used to check if the value is assigned

Dim Answer As Nullable(Of Boolean)

Dim RetVal ' As MsgBoxResult

RetVal = MsgBox("Have you booked the ticket for olympics", vbYesNoCancel, "DotNetDud Examples")

If RetVal = vbYes Then

Answer = True

ElseIf RetVal = vbNo Then

Answer = False

End If

'Gets a value indicating whether the current Nullable<(Of <(T>)>) object has a value.

If Answer.HasValue Then

MsgBox("Question answered")

Else

MsgBox("Question not answered")

End If

' The above checks if the question is answered or not. Here the boolean variable answer can store True, False and Other


End Sub

You will get Error 'HasValue' is not a member of 'Boolean', if the variable is not declared as nullable (i.e., Dim Answer As Boolean)

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Using Timer in VB.Net


Building an Electronic Clock in VB.Net using timer class

(VB.NET ProgressBar using Timer)

Public Class Form1

Dim t As New Timer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

t.Interval = 1000

AddHandler t.Tick, AddressOf Me.t_ShowProgress

AddHandler t.Tick, AddressOf Me.t_ShowTime

t.Start()

End Sub

Private Sub t_ShowProgress()

ProgressBar1.Value += 10

If ProgressBar1.Value = 100 Then

ProgressBar1.Value = 0

End If

End Sub

Private Sub t_ShowTime()

Label1.Text = Now

End Sub

End Class

Add Handler - Associates an event with an event handler at run time. The Handles keyword and the AddHandler statement both allow you to specify that particular procedures handle particular events, but there are differences. The AddHandler statement connects procedures to events at run time. Use the Handles keyword when defining a procedure to specify that it handles a particular event.

The Tick event (this is similar to Visual Basic Timer1_Timer event) occurs when the specified timer interval has elapsed and the timer is enabled. This is based on the interval set for the Timer (1000 ms or 1 sec here)

The progress bar will be reset after it reaches 100. The timer will run till the form is closed. Since the process is synchronous only the timer event will be executed or the label will be updated. The same can also be achieved async

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Regular Expressions in Dot Net (.NET)

Finding Duplicate Words in a String using .NET Regular Expressions

Regular Expressions (REGEX) does wonders in programming. Here is a simple Regex code that identifies duplicate words like 'the the', 'something something' etc. Probably copyeditors can use this code to do the work that Microsoft Spell check does


Sub Regex_Checker_Duplicate_Words()

Dim oRegex As Regex

Dim sPattern As String

Dim oMatches As Match

Dim sText As String

sText = " the the category catastropic cat cat and rat b b "

sPattern = "\b([a-zA-Z]+)\s+\1"

oRegex = New Regex(sPattern)

oMatches = oRegex.Match(sText, sPattern)

While oMatches.Success

MsgBox(oMatches.Groups(0).Value.ToString)

oMatches = oMatches.NextMatch

End While

End Sub

Returns a new Match with the results for the next match, starting at the position at which the last match ended (at the character beyond the last matched character).

See Also
Extract Ref Links From WebPage using VB.Net Regular Expressions
Remove HTML Tags from String using .NET Regular Expressions
VB.NET Regular Expression to Check URL
VB.NET Regular Expression to Check Email Addresses
VB.NET Regular Expression to Check MAC Address
Regular Expression to Check Zip Code
Validate eMail Addresses using VB.NET Function
Regular Expressions in Dot Net (.NET)

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon