Hello. I'm still kind of new to C# so I don't know if this is a newbie question or not. I am making a small application for backing up people data. I want to list only certain directories in a listview(ie. my documents, outlook folder, folders like that). I can enter all the folders manually at runtime into the list view and it works. I was just wondering is there a way to maybe loop through the user's profile folder and automatically list these folders in a list view instead of inserting them all manually? I hope I explained that well enough.
Thanks in advance.
Loading
Josh DycusPosted Jan 20, 2011, 10:29 AM
string user = Environment.UserName; //String that holds the current logged in user name
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //string that holds the users app data path
string docpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //string that holds the users My docs path
string size;
public Backup_test()
{
InitializeComponent();
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.CheckBoxes = true;
listView1.BackColor = System.Drawing.Color.LightGray;
listView1.Columns.Add("Name", 200, HorizontalAlignment.Left);
listView1.Columns.Add("Location", 300, HorizontalAlignment.Left);
listView1.Columns.Add("Size", 120, HorizontalAlignment.Left);
ListViewItem network = new ListViewItem("Mapped Network Drives");
ListViewItem.ListViewSubItem netreg = new ListViewItem.ListViewSubItem(network, "HKEY_CURRENT_USER\\Network");
network.SubItems.Add(netreg);
listView1.Items.Add(network);
ListViewItem printers = new ListViewItem("Connected Network Printers");
ListViewItem.ListViewSubItem printreg = new ListViewItem.ListViewSubItem(printers, "HKEY_CURRENT_USER\\Printers");
printers.SubItems.Add(printreg);
listView1.Items.Add(printers);
switch (Directory.Exists(docpath))
{
case true:
ListViewItem documents = new ListViewItem("My Documents");
size = Convert.ToString(GetTotalSize(docpath));
ListViewItem.ListViewSubItem docsmb = new ListViewItem.ListViewSubItem(documents, docpath);
ListViewItem.ListViewSubItem docsizemb = new ListViewItem.ListViewSubItem(documents, size);
documents.SubItems.Add(docsmb);
documents.SubItems.Add(docsizemb);
listView1.Items.Add(documents);
break;
}
}
//
//The following two methods "GetFileSize and GetTotalSize " orginal code was taken from:
//http://www.dreamincode.net/code/snippet2690.htm
//
//
//variable to hold the final value
private static double totalSize = 0;
///
/// method to increment our total size for the directory
///
///
private static void GetFileSize(string path)
{
//get the file info for this file
FileInfo fi = new FileInfo(path);
//increment the total size
totalSize += fi.Length;
}
///
/// method for getting the total size of files in a given directory
/// along with it's sub-directories
///
///
///
public static double GetTotalSize(string directory)
{
//get all files in the main directory
string[] files = Directory.GetFiles(directory);
//loop through all the files found
foreach (string file in files)
{
//add up the size of each file
GetFileSize(file);
}
//now get all the sub-directories in this directory
string[] subDirs = Directory.GetDirectories(directory);
//loop through each of the sub-directories
foreach (string dir in subDirs)
{
try
{
//use recursion, call itself to increase the size
GetTotalSize(dir);
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine(ex.ToString());
}
}
return totalSize / Math.Pow(1024, 2) ;
}
Jean PaulPosted Jan 20, 2011, 9:52 AM
Yes. It is possible to get the user special folders using: Environment.GetFolderPath() method