Pages
-
Recent Posts
- Which Would You Rather Have: The Planet, Or A Patent?
- How Copyright Would Make The ‘Singularity’ Infringement If It Ever Arrived
- Network Analysis Reveals Apparent (And Legally Questionable) Attack On Torrent Networks
- Court Dumps Patent Lawsuit Against Tons Of Online Retailers
- Java: Jury Says Google Did Not Infringe / Comprehensive JVM Options List
Archives
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- October 2008
Meta
Category Archives: VB.NET
PHPHOST BLOG
Web Hosting Related Articles You May Need
Random File Names using .NET
It helps to have knowledge of .NET classes. I was recently working on a requirement where some files were to be generated on the fly and stored on the disk. The requirement was that the files had to be generated with random characters and random extensions, to add extra security to the files. System.IO.Path.GetRandomFileName() returns a random folder name or file name which makes the task very simple, as shown below:
C#
static void Main(string[] args){ for (int i = 1; i < 10; i++) Console.WriteLine(System.IO.Path.GetRandomFileName()); Console.ReadLine();}
VB.NET
Shared Sub Main(ByVal args() As String) For i As Integer = 1 To 9 Console.WriteLine(System.IO.Path.GetRandomFileName()) Next i Console.ReadLine()End Sub
Note: If you want to physically create some files on the disk, just pass these random names to a FileStream object inside the for loop. GetRandomFileName() does not create a physical file like GetTempFileName() does.
OUTPUT
Posted in C#, Syndicated, VB.NET
Leave a comment
Free C# and VB.NET Training Videos
When you are in the need of learning a new programming language or technology, a training video can do wonders! Here are some free C# and VB.NET Training Videos for both the novice and experienced.
So grab some popcorn this weekend, sit back and learn!
C# Training Videos
“How Do I?” Videos for Visual C#
VB.NET Training Videos
“How Do I” Videos — Visual Basic
VB.NET Training on Google Videos
VB.NET Training Videos on You Tube
MSDN Visual Basic On-Demand Webcasts
Other Free Videos
PDC 2009 Sessions – sessions presented by some of Microsoft’s best speakers.
MIX 2010 Videos – MIX10 session recordings
dnrTV – dnrTv is a fusion of a training video and an interview show. Training videos are typically sterile and one-way
DimeCasts.net – Short ~10 minutes screencasts
MSDN Screencasts – webcast that takes you step-by-step to discovering new functionality or exploring a hot developer topic, all in 10-15 minutes
Visual Studio 2010 and .NET Framework 4 Training Kit – presentations, hands-on labs, and demos
Zeollar – Technical Webcasts
WindowsClient.net – many pages of tutorials and hours of video training sessions that help you learn how to create Windows Client applications
General ASP.NET Videos – dozens of videos designed for all ASP.NET developers, from the novice to the professional.
Posted in C#, Free Learning, Syndicated, VB.NET
Leave a comment
Enum.TryParse in .NET 4.0
If you are using .NET 4.0, then Enum.TryParse<TEnum> is now provided out of the box and support flags enumeration. As given in the documentation, Enum.TryParse<> ‘Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.’
Here’s how to use it:
[Flags]enum Result { Fail = 0, Pass = 1, Grace = 2 }; static void Main(string[] args){ string a = (Result.Pass || Result.Grace).ToString(); Result b; bool success = Enum.TryParse<Result>(a, out b); Console.WriteLine("{0} = {1}", success, b); Console.ReadLine();}
OUTPUT

Posted in C#, Syndicated, VB.NET
Leave a comment
Concatenate Items of a List
Here’s a simple way to concatenate items of a List<string>
C#
static void Main(string[] args){ List<string> strList = new List<string>() { "Jake", "Brian", "Raj", "Finnie", "Carol" }; string str = string.Join(";", strList.ToArray()); Console.WriteLine(str); Console.ReadLine();}
VB.NET
Sub Main(ByVal args() As String) Dim strList As New List(Of String)() _ From {"Jake", "Brian", "Raj", "Finnie", "Carol"} Dim str As String = string.Join(";", strList.ToArray()) Console.WriteLine(str) Console.ReadLine()End Sub
OUTPUT
Posted in C#, Syndicated, VB.NET
Leave a comment
Programmatically determine Total Free Space available on your Hard Drive
The DriveInfo.AvailableFreeSpace property indicates the amount of available free space on a drive. Here’s how to use this property to programmatically retrieve the total free space of all logical drives (including CD/DVD drives) on your machine.
C#
static void Main(string[] args){ foreach (DriveInfo drive in DriveInfo.GetDrives()) { try { Console.WriteLine("{0} has {1} MB free space available", drive.RootDirectory, (drive.AvailableFreeSpace / 1024) / 1024); } catch (IOException io) { Console.WriteLine(io.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.ReadLine();}
VB.NET
Shared Sub Main(ByVal args() As String) For Each drive As DriveInfo In DriveInfo.GetDrives() Try Console.WriteLine("{0} has {1} MB free space available", _drive.RootDirectory, (drive.AvailableFreeSpace \ 1024) / 1024) Catch io As IOException Console.WriteLine(io.Message) Catch ex As Exception Console.WriteLine(ex.Message) End Try Next drive Console.ReadLine()End Sub
OUTPUT
Posted in C#, Syndicated, VB.NET
Leave a comment
Detect an Empty String in .NET 4.0
In the previous versions of .NET, we used the String.IsNullOrEmpty method to find out whether the specified string is null or an Empty string. However this method did not tell us if the string contained only whitespaces. You had to use the Trim().Length method to detect that.
.NET 4.0 makes this simpler by introducing the String.IsNullOrWhiteSpace method to find out whether a specified string is null, empty, or consists only of white-space characters.
So the following piece of code
static void Main(string[] args){ string str = new String(' ', 10); Console.WriteLine(String.IsNullOrWhiteSpace(str)); Console.ReadLine();}
will return True.
Posted in C#, Syndicated, VB.NET
Leave a comment
Generate Stronger Random Numbers using C# and VB.NET
The RNGCryptoServiceProvider Class implements a cryptographic Random Number Generator (RNG) using the implementation provided by the cryptographic service provider (CSP). Here’s how to generate random numbers using this class. Make sure you import the System.Security.Cryptography namespace before using this sample code
C#
static void GenerateRandomNumber(){ byte[] byt = new byte[4]; RNGCryptoServiceProvider rngCrypto = new RNGCryptoServiceProvider(); rngCrypto.GetBytes(byt); int randomNumber = BitConverter.ToInt32(byt, 0); Console.WriteLine(randomNumber);}
VB.NET
Shared Sub GenerateRandomNumber() Dim byt As Byte() = New Byte(4) {} Dim rngCrypto As New RNGCryptoServiceProvider() rngCrypto.GetBytes(byt) Dim randomNumber As Integer = BitConverter.ToInt32(byt, 0) Console.WriteLine(randomNumber)End Sub
Posted in C#, Syndicated, VB.NET
Leave a comment
Programmatically determine if a Windows Service is running and stop it
The ServiceController class makes it easy to retrieve information about a windows service and manipulate it. Here’s some code. Before running this example, make sure you have added a reference to System.ServiceProcess
C#
static void Main(string[] args){ try { ServiceController controller = new ServiceController("SERVICENAME"); if (controller.Status.Equals(ServiceControllerStatus.Running) && controller.CanStop) { controller.Stop(); Console.WriteLine("Service Stopped"); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); }}
VB.NET
Shared Sub Main(ByVal args() As String) Try Dim controller As New ServiceController("SERVICENAME") If controller.Status.Equals(ServiceControllerStatus.Running)_
AndAlso controller.CanStop Then controller.Stop() Console.WriteLine("Service Stopped") End If Console.ReadLine() Catch ex As Exception Console.WriteLine(ex.Message) End TryEnd Sub
Make sure you have permission to stop the service or else you will get an exception.
Posted in C#, Syndicated, VB.NET
Leave a comment
Create Nested Directories in C# and VB.NET
A lot of developers do not know that Directory.CreateDirectory() can be used to create directories and subdirectories as specified by the path. Here’s an example
C#
static void Main(string[] args){ try { Directory.CreateDirectory(@"D:\ParentDir\ChildDir\SubChildDir\"); Console.WriteLine("Directories Created"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); }}
VB.NET
Shared Sub Main(ByVal args() As String) Try Directory.CreateDirectory("D:\ParentDir\ChildDir\SubChildDir\") Console.WriteLine("Directories Created") Console.ReadLine() Catch ex As Exception Console.WriteLine(ex.Message) End TryEnd Sub
OUTPUT
Posted in C#, Syndicated, VB.NET
Leave a comment
C# 4.0 New Features, Videos, Articles and Books
I was collecting some learning material on C# 4.0 and stumbled across some useful videos, articles and documents available on C# 4.0. I am just listing a few of them. Check them out!
Samples and Documents
New features of C# 4.0 (Beta 2)
Download C# 4.0 Beta 2 samples and documents
Articles
Use C# 4.0 dynamic to drastically simplify your private reflection code
C# 4.0 Optional Parameters – Exploration
Looking Ahead to C# 4.0: Optional and Named Parameters
Books
C# 4.0 in a Nutshell: The Definitive Reference
Visual C# 2010 Recipes: A Problem-Solution Approach
Introducing .NET 4.0: with Visual Studio 2010
Training Videos
C# 4.0 Dynamic with Chris Burrows and Sam Ng
How to Use Named and Optional Arguments in Office Programming (C#)
Dynamic C# and a New World of Possibilities
Please note that C# 4.0 is in Beta as of this writing.
Posted in C#, Syndicated, VB.NET
Leave a comment
LATEST NEWS
