Hello guys. I'm doing a project now. I'm having a problem on getting specific file type from my computer using the .getfiles function.
Here's my codes.

Private Sub dlgOpen_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgOpen.FileOk
        
        Dim filepath as String = "C:\"
        dlgOpen.CheckFileExists = True
        txtOpen.Text = dlgOpen.FileName

        sizetext = "MB"

        If System.IO.Directory.Exists(filepath) Then
            Dim myDir As New System.IO.DirectoryInfo(filepath)
            For Each myFile As System.IO.FileInfo In myDir.GetFiles("*.mp3")
                Me.lblFname2.Text = myFile.FullName.ToString()
                Me.lblFtype.Text = myFile.Extension.ToString()
                Me.length = ((myFile.Length.ToString / 1024) / 1024)
                Me.length = Format(length, "0.00")
                Me.lblsize2.Text = length & " " & sizetext

            Next
        End If
    End Sub

What I wanted to happen is when the openfiledialog prompted and once I've selected the file, the file information I've specified above must show on the application as a label. But nothing appears. The file name only appears on the text box beside the open button. It should also appear above the button and the text box as a label. How could i possibly do that? And how could I select just one file at a time from a folder with 150 other files in it? When I use getFiles, it only gets all at the same time everything that's in the folder even if i've only just selected one file.

I'm hoping for someone kind to answer my query. This is for the sake of our thesis project at school. Thanks! More power!

Chellemits

Recommended Answers

All 8 Replies

If you selected only one file, you dont need a foreach loop. You use it when you have multiple files.
So what you can do is this:

public Form1()
        {
            InitializeComponent();
            label1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
            open.InitialDirectory = @"C:\";
            if (open.ShowDialog() == DialogResult.OK)
            {                
                System.IO.FileInfo fInfo = new System.IO.FileInfo(open.FileName);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("File`s specifications are:\n");
                sb.AppendLine("Full file path: " + fInfo.FullName);
                sb.AppendLine("Full file name: " + fInfo.Name);
                sb.AppendLine("File name only: " + System.IO.Path.GetFileNameWithoutExtension(fInfo.FullName));
                sb.AppendLine("File extenstion: " + fInfo.Extension.ToString().Substring(1, fInfo.Extension.Length - 1)); //removing dot infront of extension!
                sb.AppendLine("File lenght: " + (String.Format("{0:0.00} MB", (((decimal)fInfo.Length / 1024) / 1024))));
                //you can add some more file`s properties if you want simply...
                label1.Text = sb.ToString();
            }
        }

Just dont use 10 labels to write all. Use a StringBuiler class as I showed in the example.
And the calculation is a bit specific too (maybe not so much for experts), but the Lenght of the file is type of Long, so when you will double divide by 1024, you will get 0 (long does not have decimals). So box it as decimal, and it will do - as shown in example!

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                    ' Insert code to read the stream here.
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub

If you selected only one file, you dont need a foreach loop. You use it when you have multiple files.
So what you can do is this:

public Form1()
        {
            InitializeComponent();
            label1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Image Files(*.png; *.jpg; *.bmp)|*.png; *.jpg; *.bmp";
            open.InitialDirectory = @"C:\";
            if (open.ShowDialog() == DialogResult.OK)
            {                
                System.IO.FileInfo fInfo = new System.IO.FileInfo(open.FileName);
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("File`s specifications are:\n");
                sb.AppendLine("Full file path: " + fInfo.FullName);
                sb.AppendLine("Full file name: " + fInfo.Name);
                sb.AppendLine("File name only: " + System.IO.Path.GetFileNameWithoutExtension(fInfo.FullName));
                sb.AppendLine("File extenstion: " + fInfo.Extension.ToString().Substring(1, fInfo.Extension.Length - 1)); //removing dot infront of extension!
                sb.AppendLine("File lenght: " + (String.Format("{0:0.00} MB", (((decimal)fInfo.Length / 1024) / 1024))));
                //you can add some more file`s properties if you want simply...
                label1.Text = sb.ToString();
            }
        }

Hi Mitja, I'm still having a problem with my codes. It says that "Object reference not set to an instance of an object."

Dim filepath As String = "C:\"
    Dim myFile As New System.IO.FileInfo(filepath)

 Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click

        dlgOpen.InitialDirectory = filepath
        dlgOpen.Title = "Please Select a File"
        dlgOpen.Filter = " Audio Files (*.mp3); (*.mp4); (*.wav); (*.wmv); (*.mov)|*.mp3; *.mp4; *.wav; *.wmv; *.mov | Video Files (*.flv); (*.avi); (*.3gp); (*.mov)| *.flv; *.avi; *.3gp; *.mov| All Files (*.*)|*.*"

        dlgOpen.ShowDialog()
        sizetext = "MB"
        If dlgOpen.ShowDialog = DialogResult.OK Then
            Dim myDir As New System.IO.DirectoryInfo(filepath)
            myDir.GetFiles()
            Me.lblFname2.Text = myFile.FullName.ToString
            Me.lblFtype.Text = myFile.Extension.ToString
            Me.length = ((myFile.Length.ToString / 1024) / 1024)
            Me.length = Format(length, "0.00")
            Me.lblsize2.Text = length & " " & sizetext
        End If

    End Sub

I'm using vb.net 2008 by the way...

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                    ' Insert code to read the stream here.
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub

Chibex64: I'm having a problem on getting the fileinfo. The problem is stated above. I can't seem to figure out what's wrong. I'm kinda new to developing an audio and video file converter. :( Can you help me, please?

What file Info? the file name or file extension?

What file Info? the file name or file extension?

both...

Chibex64: I'm having a problem on getting the fileinfo. The problem is stated above. I can't seem to figure out what's wrong. I'm kinda new to developing an audio and video file converter. :( Can you help me, please?

Would you look into my example code? There is explained all in the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.