System.IO Project for beginners
-
Im posting this to see what people come up with in this project. I dont need help with this but am in the process of trying to figure out system.io. Ive started the project off which is a windows form with a Get Folders and Files button which then opens a FolderBrowserDialog to let you pick the directory you want to list on the listbox with its subdirectories and files. 1) How can we rename a certain directory listed in the listbox 2) How do we remove unwanted directories 3) How do we check for duplicate files or directories, and remove one) 4) How do we remove unwanted characters in directory or file names. Feel free to add other points that we could do with this project.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace MusicProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void btnGetFolders\_Click(object sender, EventArgs e) { listBox1.Items.Clear(); //To get the FolderBrowserDialog to choose the correct folder. FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //To get all directories in the one you selected. foreach (string dir in Directory.GetDirectories(fbd.SelectedPath)) { DirectoryInfo dInfo = new DirectoryInfo(dir); this.listBox1.Items.Add(dInfo.Name); } //To get all the files in the directory you selected. foreach (string file in Directory.GetFiles(fbd.SelectedPath)) { FileInfo fInfo = new FileInfo(file); this.listBox1.Items.Add (fInfo.Name); } } } }
}
-
Im posting this to see what people come up with in this project. I dont need help with this but am in the process of trying to figure out system.io. Ive started the project off which is a windows form with a Get Folders and Files button which then opens a FolderBrowserDialog to let you pick the directory you want to list on the listbox with its subdirectories and files. 1) How can we rename a certain directory listed in the listbox 2) How do we remove unwanted directories 3) How do we check for duplicate files or directories, and remove one) 4) How do we remove unwanted characters in directory or file names. Feel free to add other points that we could do with this project.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace MusicProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void btnGetFolders\_Click(object sender, EventArgs e) { listBox1.Items.Clear(); //To get the FolderBrowserDialog to choose the correct folder. FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //To get all directories in the one you selected. foreach (string dir in Directory.GetDirectories(fbd.SelectedPath)) { DirectoryInfo dInfo = new DirectoryInfo(dir); this.listBox1.Items.Add(dInfo.Name); } //To get all the files in the directory you selected. foreach (string file in Directory.GetFiles(fbd.SelectedPath)) { FileInfo fInfo = new FileInfo(file); this.listBox1.Items.Add (fInfo.Name); } } } }
}
-
Im posting this to see what people come up with in this project. I dont need help with this but am in the process of trying to figure out system.io. Ive started the project off which is a windows form with a Get Folders and Files button which then opens a FolderBrowserDialog to let you pick the directory you want to list on the listbox with its subdirectories and files. 1) How can we rename a certain directory listed in the listbox 2) How do we remove unwanted directories 3) How do we check for duplicate files or directories, and remove one) 4) How do we remove unwanted characters in directory or file names. Feel free to add other points that we could do with this project.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace MusicProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void btnGetFolders\_Click(object sender, EventArgs e) { listBox1.Items.Clear(); //To get the FolderBrowserDialog to choose the correct folder. FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { //To get all directories in the one you selected. foreach (string dir in Directory.GetDirectories(fbd.SelectedPath)) { DirectoryInfo dInfo = new DirectoryInfo(dir); this.listBox1.Items.Add(dInfo.Name); } //To get all the files in the directory you selected. foreach (string file in Directory.GetFiles(fbd.SelectedPath)) { FileInfo fInfo = new FileInfo(file); this.listBox1.Items.Add (fInfo.Name); } } } }
}
bdeklerk wrote:
Feel free to add other points that we could do with this project.
A forum-post would not be the correct place for a longer-term project. Turn it into an article, and people could collaborate. You already have answers to the other questions, which leaves #3 for me;
bdeklerk wrote:
- How do we check for duplicate files or directories, and remove one)
You can iterate over all files and folders, and for each item; calculate a hash (like md5) and put it in a dictionary, with the hash as the key and the path as the value. If you hit a double key, you'll know that the content of both files is the same - even if the filenames and/or timestamps on the files differ. Good luck :)
Bastard Programmer from Hell :suss: