Click here to Skip to main content
16,011,120 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: How to convert VB.net dll to be visible in vb6.0 Pin
Christian Graus25-Apr-07 1:47
protectorChristian Graus25-Apr-07 1:47 
Questiondata view related query Pin
Anz Ananz24-Apr-07 22:05
Anz Ananz24-Apr-07 22:05 
AnswerRe: data view related query Pin
Pankaj.Jain24-Apr-07 22:33
professionalPankaj.Jain24-Apr-07 22:33 
Questionhow to use image edit in vb. net ? Pin
babusat24-Apr-07 21:59
babusat24-Apr-07 21:59 
AnswerRe: how to use image edit in vb. net ? Pin
Dave Kreskowiak25-Apr-07 4:02
mveDave Kreskowiak25-Apr-07 4:02 
GeneralRe: how to use image edit in vb. net ? Pin
babusat25-Apr-07 17:29
babusat25-Apr-07 17:29 
GeneralRe: how to use image edit in vb. net ? Pin
Dave Kreskowiak26-Apr-07 13:39
mveDave Kreskowiak26-Apr-07 13:39 
Questionaccess denied error Pin
alpdoruk24-Apr-07 21:38
alpdoruk24-Apr-07 21:38 
i have a program that show the size of programs but i want something to add to the program but i have no idea so if the program gives the access denied error it stops and i don't want that it stops and it does the next file how and where should i have to add these files.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DirectorySize
{
public partial class Form1 : Form
{
delegate void SetStatusbarCallback(string text);

private DataTable m_dirs = new DataTable();
private Dictionary m_dirsSize = new Dictionary();

#region Form
public Form1()
{
InitializeComponent();
}

private void Form1_Resize(object sender, EventArgs e)
{
if (Form1.ActiveForm != null)
{
grdDirectories.Width = (Form1.ActiveForm.Width / 2) - 4;
grdFiles.Width = (Form1.ActiveForm.Width / 2) - 4;
grdFiles.Left = (Form1.ActiveForm.Width / 2);
}
}

private void Form1_Load(object sender, EventArgs e)
{
this.Width = Properties.Settings.Default.ProgramWidth;
this.Height = Properties.Settings.Default.ProgramHeight;

if (Properties.Settings.Default.ProgramLeft > -100)
{
this.Left = Properties.Settings.Default.ProgramLeft;
this.Top = Properties.Settings.Default.ProgramTop;
}

txtFolder.Text = Properties.Settings.Default.Directory;

grdDirectories.Width = (this.Width / 2) - 4;
grdFiles.Width = (this.Width / 2) - 4;
grdFiles.Left = (this.Width / 2);
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.ProgramWidth = this.Width;
Properties.Settings.Default.ProgramHeight = this.Height;
Properties.Settings.Default.ProgramLeft = this.Left;
Properties.Settings.Default.ProgramTop = this.Top;

Properties.Settings.Default.Save();
}
#endregion

#region Scan Directory
#region Buttons
private void btnScan_Click(object sender, EventArgs e)
{
if (!Directory.Exists(txtFolder.Text))
{
MessageBox.Show("The folder does not exist.");
txtFolder.Focus();
return;
}

Properties.Settings.Default.Directory = txtFolder.Text;
Properties.Settings.Default.Save();

btnCancel.Enabled = true;
btnScan.Enabled = false;
txtFolder.Enabled = false;
toolStripProgressBar1.Visible = true;
toolStripProgressBar1.Value = 0;

this.Cursor = Cursors.WaitCursor;

this.backgroundWorker1.RunWorkerAsync();
}

private void btnBrowse_Click(object sender, EventArgs e)
{
if (txtFolder.Text.Trim() != "")
folderBrowserDialog1.SelectedPath = txtFolder.Text.Trim();

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
txtFolder.Text = folderBrowserDialog1.SelectedPath;
}

private void btnCancel_Click(object sender, EventArgs e)
{
this.btnCancel.Enabled = false;
this.backgroundWorker1.CancelAsync();
}
#endregion

///
/// Controls in Windows Forms are bound to a specific thread and are not thread safe.
/// Therefore, if you are calling a control's method from a different thread, you must
/// use one of the control's invoke methods to marshal the call to the proper thread.
///
///
private void SetStatusBar(string text)
{
if (this.statusStrip1.InvokeRequired)
{
SetStatusbarCallback d = new SetStatusbarCallback(SetStatusBar);
this.Invoke(d, new object[] { text });
}
else
{
this.toolStripStatusLabel1.Text = text;
}
}

///
/// The method that does all the work.
///
/// The directory to search from.
///
private bool LoopFolder(DirectoryInfo[] dis)
{
if (this.backgroundWorker1.CancellationPending)
return false;

foreach (DirectoryInfo di in dis)
{
if (this.backgroundWorker1.CancellationPending)
return false;

string fullname = di.FullName;

SetStatusBar(fullname);

double size = GetFilesSize(di);

DataRow dr = m_dirs.NewRow();

dr["Name"] = fullname;
dr["Size"] = size;

m_dirs.Rows.Add(dr);
m_dirsSize.Add(fullname, size);

// sum up the fullsize on all parents
DirectoryInfo parent = di.Parent;
while (parent != null && !this.backgroundWorker1.CancellationPending)
{
if (m_dirsSize.ContainsKey(parent.FullName))
{
m_dirsSize[parent.FullName] += size;
parent = parent.Parent;
}
else
break;
}

toolStripProgressBar1.PerformStep();
}

return true;
}

///
/// Gets the total size of all files in the directory.
///
///
///
private double GetFilesSize(DirectoryInfo di)
{
double size = 0.0;
long lSize = 0;

foreach (FileInfo fi in di.GetFiles())
lSize += fi.Length;

size = Convert.ToDouble(lSize) / 1024.0 / 1024.0; // size in MB

return size;
}

#region BackgroundWorker
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Do not access the form's BackgroundWorker reference directly.
// Instead, use the reference provided by the sender parameter.
BackgroundWorker bw = sender as BackgroundWorker;

// Extract the argument.
//string[] args = (string[])e.Argument;

m_dirs = new DataTable();
m_dirsSize.Clear();

m_dirs.Columns.Add("Name", typeof(string));
m_dirs.Columns.Add("Size", typeof(double));
m_dirs.Columns.Add("Fullsize", typeof(double));

// Start the time-consuming operation.
DirectoryInfo di = new DirectoryInfo(txtFolder.Text);
DirectoryInfo[] dis = di.GetDirectories("*", SearchOption.AllDirectories);

toolStripProgressBar1.Maximum = dis.Length;

e.Result = LoopFolder(dis);

// If the operation was canceled by the user,
// set the DoWorkEventArgs.Cancel property to true.
if (bw.CancellationPending)
{
e.Cancel = true;
}
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnCancel.Enabled = false;
btnScan.Enabled = true;
txtFolder.Enabled = true;
toolStripProgressBar1.Visible = false;

this.Cursor = Cursors.Default;

if (e.Cancelled)
{
// The user canceled the operation.
MessageBox.Show("Operation was canceled");
}
else if (e.Error != null)
{
// There was an error during the operation.
string msg = String.Format("An error occurred: {0}", e.Error.Message);
MessageBox.Show(msg);
}
else
{
foreach (DataRow dr in m_dirs.Rows)
{
dr["Size"] = Math.Round((double)dr["Size"], 2);

string fullname = (string)dr["Name"];

if (m_dirsSize.ContainsKey(fullname))
dr["Fullsize"] = Math.Round(m_dirsSize[fullname], 2);
}

m_dirs.DefaultView.Sort = "Size desc";
grdDirectories.DataSource = m_dirs;
toolStripStatusLabel1.Text = m_dirsSize.Count + " directories found.";
}
}
#endregion
#endregion

#region Show files
private void grdDirectories_SelectionChanged(object sender, EventArgs e)
{
if (grdDirectories.SelectedRows.Count == 1)
{
string directory = grdDirectories.SelectedRows[0].Cells[0].Value.ToString();

toolStripStatusLabel1.Text = directory;

DataTable tblFiles = new DataTable();

tblFiles.Columns.Add("Name", typeof(string));
tblFiles.Columns.Add("Size", typeof(double));
tblFiles.Columns.Add("LastAccessTime", typeof(DateTime));
tblFiles.Columns.Add("LastWriteTime", typeof(string));
tblFiles.Columns.Add("CreationTime", typeof(string));
tblFiles.Columns.Add("ReadOnly", typeof(bool)).ReadOnly = true;
tblFiles.Columns.Add("Ext", typeof(string));

DirectoryInfo di = new DirectoryInfo(directory);

foreach (FileInfo fi in di.GetFiles())
{
DataRow dr = tblFiles.NewRow();

dr["Name"] = fi.Name;
dr["LastAccessTime"] = fi.LastAccessTime;
dr["Size"] = Math.Round(fi.Length / 1024.0 / 1024.0, 2); // size in MB
dr["LastWriteTime"] = fi.LastWriteTime;
dr["ReadOnly"] = fi.IsReadOnly;
dr["CreationTime"] = fi.CreationTime;
dr["Ext"] = fi.Extension;

tblFiles.Rows.Add(dr);
}

tblFiles.DefaultView.Sort = "Size desc";

grdFiles.DataSource = tblFiles;
}
}
#endregion

}
AnswerRe: access denied error Pin
Dave Kreskowiak25-Apr-07 3:59
mveDave Kreskowiak25-Apr-07 3:59 
AnswerRe: access denied error Pin
alpdoruk27-Apr-07 1:23
alpdoruk27-Apr-07 1:23 
Questionhow to specify location of saving file NETCF Pin
laurensia inge24-Apr-07 21:17
laurensia inge24-Apr-07 21:17 
AnswerRe: how to specify location of saving file NETCF Pin
Suhail Shahab24-Apr-07 21:33
Suhail Shahab24-Apr-07 21:33 
GeneralRe: how to specify location of saving file NETCF Pin
Dave Kreskowiak25-Apr-07 3:51
mveDave Kreskowiak25-Apr-07 3:51 
GeneralRe: how to specify location of saving file NETCF Pin
Suhail Shahab25-Apr-07 19:08
Suhail Shahab25-Apr-07 19:08 
GeneralRe: how to specify location of saving file NETCF Pin
Suhail Shahab25-Apr-07 19:15
Suhail Shahab25-Apr-07 19:15 
GeneralRe: how to specify location of saving file NETCF Pin
Dave Kreskowiak26-Apr-07 13:41
mveDave Kreskowiak26-Apr-07 13:41 
GeneralRe: how to specify location of saving file NETCF Pin
laurensia inge26-Apr-07 22:36
laurensia inge26-Apr-07 22:36 
Questionhow we hide destop and task Bar in asp.net and vb.net Pin
Suhail Shahab24-Apr-07 21:12
Suhail Shahab24-Apr-07 21:12 
AnswerRe: how we hide destop and task Bar in asp.net and vb.net Pin
Sathesh Sakthivel25-Apr-07 0:23
Sathesh Sakthivel25-Apr-07 0:23 
GeneralRe: how we hide destop and task Bar in asp.net and vb.net Pin
Suhail Shahab25-Apr-07 19:27
Suhail Shahab25-Apr-07 19:27 
AnswerRe: how we hide destop and task Bar in asp.net and vb.net Pin
Dave Kreskowiak25-Apr-07 3:49
mveDave Kreskowiak25-Apr-07 3:49 
GeneralRe: how we hide destop and task Bar in asp.net and vb.net Pin
Suhail Shahab25-Apr-07 19:20
Suhail Shahab25-Apr-07 19:20 
GeneralRe: how we hide destop and task Bar in asp.net and vb.net Pin
Dave Kreskowiak26-Apr-07 13:42
mveDave Kreskowiak26-Apr-07 13:42 
QuestionReports to EXCEL Pin
venkata lakshmi prasanna24-Apr-07 20:04
venkata lakshmi prasanna24-Apr-07 20:04 
QuestionSetting Wrap text for a label in VB.Net Pin
venkata lakshmi prasanna24-Apr-07 20:02
venkata lakshmi prasanna24-Apr-07 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.