Reading a files properties.

How do you read the properties of files on your computer. For example, an mp3's title, genre, or a video's compression or dimensions.

Thanks.




Answer this question

Reading a files properties.

  • Zano

    Thank you, you have been very helpful.

  • pipemx

    For basic file properties, like creation date, file size etc. you can use information from : System.IO to get some info.

    (remember to include Imports System.IO at the top of your class)

    Add a OpenFileDialog and a button to your form and put this in the Click_Event:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim fData As String

    Dim infoReader As System.IO.FileInfo

    OpenFileDialog1.ShowDialog()

    fData = OpenFileDialog1.FileName

    infoReader = My.Computer.FileSystem.GetFileInfo(fData)

    MsgBox("File was created on " & infoReader.CreationTime & " " & infoReader.Attributes.ToString)

    End Sub

    For extended info on a file, like an mp3 or video file, you will have to write a bit of serious code. Check this link in : MSHelp,

    ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.SQLADD.v10.en/dnsse/html/sse_manmusiccoll.htm

    Here is just a portion of the code to access the mp3 metadata:

    Option Strict On

    Imports System.IO
    Imports System.Text

    Friend Class ID3v1Reader
    Private m_Artist As String
    Private m_Comment As String
    Private m_Genre As Byte = 255
    Private m_FileName As String
    Private m_Recording As String
    Private m_TrackSequence As Byte = 0
    Private m_TrackTitle As String
    Private m_Year As String

    Public Property Artist() As String
    Get
    Return Me.m_Artist
    End Get
    Set(ByVal Value As String)
    Me.m_Artist = Value
    End Set
    End Property
    Public Property Comment() As String
    Get
    Return Me.m_Comment
    End Get
    Set(ByVal Value As String)
    Me.m_Comment = Value
    End Set
    End Property
    Public Property Genre() As Byte
    Get
    Return Me.m_Genre
    End Get
    Set(ByVal Value As Byte)
    Me.m_Genre = Value
    End Set
    End Property
    Public Property FileName() As String
    Get
    Return Me.m_FileName
    End Get
    Set(ByVal Value As String)
    Me.m_FileName = Value
    Me.Reset()
    End Set
    End Property
    Public Property Recording() As String
    Get
    Return Me.m_Recording
    End Get
    Set(ByVal Value As String)
    Me.m_Recording = Value
    End Set
    End Property
    Public Property TrackSequence() As Byte
    Get
    Return Me.m_TrackSequence
    End Get
    Set(ByVal Value As Byte)
    Me.m_TrackSequence = Value
    End Set
    End Property
    Public Property TrackTitle() As String
    Get
    Return Me.m_TrackTitle
    End Get
    Set(ByVal Value As String)
    Me.m_TrackTitle = Value
    End Set
    End Property
    Public Property Year() As String
    Get
    Return Me.m_Year
    End Get
    Set(ByVal Value As String)
    Me.m_Year = Value
    End Set
    End Property
    Public Sub New()
    ' This is needed since there is a
    ' paramaterized constructor.
    ' This is so this class can be used
    ' for multiple files.
    End Sub
    Public Sub New(ByVal FileName As String)
    Me.FileName = FileName
    End Sub
    Public Sub Parse()
    Debug.Assert(Me.FileName IsNot Nothing)
    Debug.Assert(File.Exists(Me.FileName))

    Dim fs As FileStream = Nothing
    Dim ae As ASCIIEncoding
    Dim buffer(127) As Byte
    Dim tag As String
    Dim hdr As String

    Try
    fs = New FileStream(Me.FileName, _
    FileMode.Open, FileAccess.Read)
    ae = New ASCIIEncoding

    fs.Seek(-128, SeekOrigin.End)
    fs.Read(buffer, 0, 128)

    tag = ae.GetString(buffer)
    hdr = tag.Substring(0, 3)

    If hdr = "TAG" Then
    Me.TrackTitle = SubStringNull(tag, 3, 30)
    Me.Artist = SubStringNull(tag, 33, 30)
    Me.Recording = SubStringNull(tag, 63, 30)
    Me.Year = SubStringNull(tag, 93, 4)
    Me.Comment = SubStringNull(tag, 97, 30)

    If buffer(125) = 0 Then
    Me.TrackSequence = buffer(126)
    Me.Genre = buffer(127)
    End If
    End If

    Catch ex As Exception
    MessageBox.Show(ex.Message, ex.Source, _
    MessageBoxButtons.OK, MessageBoxIcon.Error)

    Finally
    If Not fs Is Nothing Then
    fs.Flush()
    fs.Close()
    End If
    End Try
    End Sub
    Private Sub Reset()
    Me.Artist = Nothing
    Me.Comment = Nothing
    Me.Genre = 255
    Me.Recording = Nothing
    Me.TrackSequence = 0
    Me.TrackTitle = Nothing
    Me.Year = Nothing
    End Sub
    Private Function SubStringNull( _
    ByVal s As String, ByVal start As Integer, _
    ByVal length As Integer) As String

    Dim subS As String = s.Substring(start, length)
    Return subS.TrimEnd(Convert.ToChar(0))
    End Function
    End Class

    And for video file info, check out Bob Powell's GDI+ FAQ at:

    http://www.bobpowell.net/discoverproperties.htm

    That one is a C# example. It can be converted to VB with a bit of work. Have fun!!

    james

    aka:Trucker


  • Reading a files properties.