Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
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 26-Jun-12 13:01pm
Comments
Sergey Alexandrovich Kryukov at 26-Jun-12 18:07pm
   
Not a question. What's your problem here?
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can get the directory size with linq:
 
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:
 
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
string path1 =
    @"C:\Users\cnelson\Documents\Visual Studio 2010\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs";
  Permalink  
v3
Comments
Wonde Tadesse at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100