dotnet thoughts 

a dotnet developer's technical blog

Getting File size in .net 2.0

Code to get size of a file in bytes

Public Shared Function getFileSize(ByVal Filename as string) as Integer
return IO.File.ReadAllBytes(Filename).Length
End Function

I got some additional comments from my friend, Prasanth, to Get the File Version, using the System.Diagnostics namespace.

System.Diagnostics.FileVersionInfo.GetVersionInfo(FileName.ToString).FileVersion.ToString

Thanks to Prasanth.

Generics in C# 2.0

Generics refer to classes and methods that work uniformly on values of different types. Generics allow you to define type-safe classes without compromising type safety, performance, or productivity. You implement the server only once as a generic server, while at the same time you can declare and use it with any type. To do that, use the < and > brackets, enclosing a generic type parameter.

Here is some sample implementation. It is Generic Stack class already available in System.Collections.Generic

public class Stack<T>
{
    T[] items;
    int currentIndex = 0;
    public Stack(int size)
    {
        items = new T[size];
    }
    public void Push(T item)
    {
        this.items[currentIndex] = item;
        this.currentIndex++;
    }
    public T Pop()
    {
        if (this.currentIndex < 0)
        {
            throw new IndexOutOfRangeException();
        }
        return this.items[this.currentIndex];
    }
}

And you can use the stack like this.

Stack<int> stack = new Stack<int>(10);
stack.Push(100);
int number = stack.Pop();

For more information check this URL :
http://msdn2.microsoft.com/en-us/library/ms379564(VS.80).aspx

Regular expression to remove HTML tags in VB.Net

Some time we may need to strip all the HTML tags from HTML files, like if we are doing some web scrapping. Here is simple Regular expression which will remove all the HTML Tags.

Public Sub RemoveHTMLTags(ByVal expression as string) as string
Dim pattern As String = "<(.|\n)*?>;"
Return System.Text.RegularExpressions.Regex.Replace(strHtmlString, pattern, String.Empty).Trim()
End Sub

Disable right click with Javascript

Here is two javascript code, helps you to disable right click, on webpages with Javascript.

function NoRightClick() {
if (event.button == 2)
{
alert("This action is not possible")
}
}
document.onmousedown=NoRightClick;

Or you can simply use.

<body oncontextmenu="return(false);">

This second code works only in Microsoft Internet Explorer.

Font Enumeration in VB.Net

If you are developing some editors is .Net, you have to list all the Fonts in you system in a Font Combo. Here is a simple code snippet which will display all the Windows Font in a Dropdown list.

For Each MyFont As FontFamily In System.Drawing.FontFamily.Families
Dropdownlist.Items.Add(MyFont.Name)
Next

It is in C#

foreach (FontFamily item in System.Drawing.FontFamily.Families)
{
this.comboBox1.Items.Add(item.Name);
}

Here is an Update. You can render the Dropdown such way that is will display all the font name, in the Font.

ComboBox lst = (ComboBox)sender;
e.DrawBackground();
Font font = new Font(lst.Items[e.Index].ToString(), 12);
e.Graphics.DrawString(lst.Items[e.Index].ToString(), font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();

You need to write this code in the DrawItem event of the Combo box. Also you need to change the DrawMode property of the Combo box.

Older Posts »