Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help! I just want to get the total size of a directory and display that size in a progress bar with a limit of 700mb.

Everytime you add an .mp3 file to my listbox, the files are automatically converted to .wav, added to the list, and then I want the progress bar to represent the file size of the directory, with the progrssbars max value at 700mb. When the directory gets to 700mb or greater I would likethe progressbar to turn red and display a message. the Directory I want to get the size of is C:\Program Files\3Dream Labs\DreamPlayR\mp3Temp

here is my code so far:

C++
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using NAudio.Wave;

namespace BurnR_Playlist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            createCDcache();
            LoadAudioFile1();
            ConvertMp3toWav();
            add2playlist();
            updateSize();
        }

        private void LoadAudioFile1()
        {
            openFileDialog1.Filter = "MP3 Music (*.mp3)|*.mp3;";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                string newFileName = System.IO.Path.GetFileNameWithoutExtension(textBox1.Text);
                textBox2.Text = textBox3.Text + string.Format(@"\{0}.wav", newFileName);
                textBox4.Text = newFileName;
            }
        }

        private void ConvertMp3toWav()
        {
            using (Mp3FileReader mp3 = new Mp3FileReader(openFileDialog1.FileName))
            {
                using (WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(mp3))
                {
                    WaveFileWriter.CreateWaveFile(textBox2.Text, pcm);
                }
            }
        }

        private void add2playlist()
        {
            listBox2.Items.Add(textBox4.Text);
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
                MessageBox.Show("No tracks to remove.");
            else
                removeMp3();
            updateSize();
        }




        private void removeMp3()
        {
            string mpath = textBox5.Text + listBox2.SelectedItem.ToString() + ".wav";
            textBox6.Text = mpath;
            string path = (textBox6.Text);
            File.Delete(path);
            listBox2.Items.Remove(listBox2.SelectedItem);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox2.Items.Count == 0)
                createCDcache();

            else
                deleteCDcache();
            listBox2.Items.Clear();
        }

        private void deleteCDcache()
        {
            Directory.Delete(textBox5.Text, true);
        }

        private void createCDcache()
        {
            Directory.CreateDirectory(@"C:\Program Files\3Dream Labs\DreamPlayR\mp3Temp");
        }

        private void updateSize()
        {
            
        }

        

    }

}
Posted
Comments
Sergey Alexandrovich Kryukov 26-Jun-12 18:07pm    
Not a question. What's your problem here?
--SA

1 solution

You can get the directory size with linq:

C#
string path1 =
    @"C:\Users\cnelson\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2";
var dir = new DirectoryInfo(path1);
long totalsize = dir.GetFiles().Sum(file => file.Length);


or do it the old way:

C#
string path1 =
    @"C:\Users\cnelson\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2";
var dir = new DirectoryInfo(path1);
long totalsize = 0;
foreach (var file in dir.GetFiles())
    totalsize += file.Length;


[EDITED]
path was written as file path like
C#
string path1 =
    @"C:\Users\cnelson\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs";
 
Share this answer
 
v3
Comments
Wonde Tadesse 26-Jun-12 19:37pm    
Answer updated as shown in the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900