Saturday, October 29, 2011

Getting Mimetype .NET


Private Function MimeType(Filename As String) As String
 Dim mime As String = "application/octetstream"
 Dim ext As String = System.IO.Path.GetExtension(Filename).ToLower()
 Dim rk As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext)
 If rk IsNot Nothing AndAlso rk.GetValue("Content Type") IsNot Nothing Then
  mime = rk.GetValue("Content Type").ToString()
 End If
 Return mime
End Function
In short - you just feed it with path top a file - and get back the mime type. Ex MimeType("f:\\theimage.jpg") --->>> "image/jpeg"

Sunday, October 16, 2011

Check For 64bit Operating System VB.NET 2.0

    Public Function Is64BitOperatingSystem() As Boolean?
        Dim amount As Integer
        Try
            amount = Runtime.InteropServices.Marshal.SizeOf(IntPtr.Zero)
            If amount = 8 Then : Return True
            Else : Return False
            End If
        Catch ex As Exception
            Return Nothing
        End Try
    End Function 'End Method Is64BitOperatingSystem

Wednesday, October 12, 2011

Count Lines in File


The fastest solution + holds only 1 line at a time in the memory (best solution)

Private Function TotalLines(filePath As String) As Integer
 Using r As New StreamReader(filePath)
  Dim i As Integer = 0
  While r.ReadLine() IsNot Nothing
   i += 1
  End While
  Return i
 End Using
End Function