listview opening directories - c#

I'm very new to C#. My boss asked me to wrote some code using listview as a file browser. I tried it and it seems it works. This code is to open the files from your drives and display it on the listView. It's very simple. I also made an additional function where one can clear the displayed items in the listView. I would like to add additional feature, where I can also open the directory and not only the files.
By the way, here is the sample of my code:
private void btnOpen_Click(object sender, EventArgs e)
{
string strSelectedPath;
folderBrowserDialog1.ShowDialog();
strSelectedPath = folderBrowserDialog1.SelectedPath;
label1.Text = strSelectedPath;
DirectoryInfo di = new DirectoryInfo(strSelectedPath);
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
label1.Text="";
}
Could you please show me how?

If I understand your question correctly (you want to list not only the files in the selected directory, but also the sub-directories), you will want to look into the GetDirectories method of the DirectoryInfo class.
You could do something like this, perhaps:
DirectoryInfo di = new DirectoryInfo(strSelectedPath);
// first list sub-directories
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
listView1.Items.Add(dir.Name);
}
// then list the files
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
Update: I would suggest that you move the above code into a separate method that takes a string parameter for the path (something like ListDirectoryContents(string path)). In this method you can start with clearing the items from the list view, setting the label text and then adding new content as above.
This method can be called from your btnOpen_Click method, passing folderBrowserDialog1.SelectedPath in the path parameter.
I usually try to keep my event handlers as small as possible, preferably not performing any real work but rather just call some other method that does the work. This opens up a bit more for triggering the same functionality from other places.
Say for instance that your application can take a path as a command line parameter, then it will be cleaner code just calling ListDirectoryContents with the path from the command line than perhaps duplicating the same behaviour in your form.
Full code example of that approach:
private void btnOpen_Click(object sender, EventArgs e)
{
string path = BrowseForFolder();
if (path != string.Empty)
{
ListDirectoryContent(path);
}
}
private string BrowseForFolder()
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
return fbd.SelectedPath;
}
return string.Empty;
}
private void ListDirectoryContent(string path)
{
label1.Text = path;
listView1.Items.Clear();
DirectoryInfo di = new DirectoryInfo(path);
// first list sub-directories
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
listView1.Items.Add(dir.Name);
}
// then list the files
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
}
The upsides of this code is that you can reuse the method BrowseForFolder whenever you need to browse for a folder, since it simply returns the selected path and is not connected to what the path will be used for. Similarly, the ListDirectoryContent method is completely unaware of from where the path parameter comes; it may come from the folder browser, it may come from the command line or anywhere else.
If you focus on having your methods performing only their "core task", relying on input parameters for any additional information that they need you will get code that is easier to reuse and maintain.
When it comes to event handlers (such as btnOpen_Click), I like to see them as triggers; they trigger things to happen, but the don't really do a lot themselves.

The DirectoryInfo class contains a GetDirectories method, as well as a GetFiles method. See your original code sample below, modified to add directories:
private void btnOpen_Click(object sender, EventArgs e)
{
string strSelectedPath;
folderBrowserDialog1.ShowDialog();
strSelectedPath = folderBrowserDialog1.SelectedPath;
label1.Text = strSelectedPath;
DirectoryInfo di = new DirectoryInfo(strSelectedPath);
FileInfo[] files = di.GetFiles();
DirectoryInfo[] directories = di.GetDirectories();
foreach (DirectoryInfo directory in directories)
{
listView1.Items.Add("Directory " + directory.Name);
}
foreach (FileInfo file in files)
{
listView1.Items.Add(file.Name);
}
}

I believe this is what you want?
DirectoryInfo[] directories = di.GetDirectories();
foreach (DirectoryInfo directory in directories)
{
listView1.Items.Add(directory.Name);
}

Related

c# How can I pass a value from child back to parent?

I have this project where I'm iterating recursively in some folders and copy them to a new directory.
In the 2nd level of folders (child folder) I have a txt file which I am reading. Now, when I read that file, I parse a string and I want to give that string to the parent folder and assign it as its name. So far I can only do for its File only, because there is accessible but when I try to do it for the parent folder, it doesn't recognize the variable (which of course, makes sense!)
EDIT: I commented the code so it will be a little bit clearer.
Now, Is there any way I can achieve this?
Here's my code:
static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
//accessing the directiories inse of Destionationed directory
DirectoryInfo[] dirs = dir.GetDirectories();
Directory.CreateDirectory(destinationDir);
if (recursive)
{
// reading every folder inside first parent folder
foreach (DirectoryInfo subDir in dirs)
{
//reading every folder inside the second folder
DirectoryInfo[] Subdirs = subDir.GetDirectories();
foreach (DirectoryInfo subDir2 in Subdirs)
{
//setting the new path where I want my new files to be stored.
string newDestinationDir = Path.Combine(destinationDir, subDir2.Name);
//Now, here its where I want to change the main folder's name, but the value must be taking from inside it's file.
CopyDirectory(subDir2.FullName, newDestinationDir, true);
FileInfo[] SubDirsFiles = subDir2.GetFiles();
foreach (FileInfo getFile in SubDirsFiles)
{
File.Copy(getFile.FullName, newDestinationFile + "_" + dictionary["XECM_DOC_TYPE"], true);
}
}
}
}
}
Note: I'm using Dictiony to story the data I read from file, and thats what I'm trying to pass to the parent folder, exclusively-to the CopyDirectory(subDir2.FullnName,NewDestinationDir+dictionary["XECM_DOC_TYPE"],true) but logically it can't recognize the dictionary part because it is declared and initialized inside the FileInfo part...
I hope I was clear enough.

If else DirectoryInfo

i used DirectoryInfo to get files from a folder.
but let say the directory to the folder does not exist
i want a message that says ("directory not found")
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
FileInfo[] Files = dinfo.GetFiles("*.DOCX");
foreach (FileInfo file in Files)
{
LB2.Items.Add(file.Name);
}
Use Exists method of DirectoryInfo class to check to check whether directory exists or not.
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
if(dinfo.Exists)
{
//your code
}
The file-system can change under your feet, so it's generally better to make an attempt with the file-system and take appropriate corrective measures if you encounter an exception.
So instead of testing with dinfo.Exists then crossing your fingers that the same situation persists on the next few lines, just go ahead and try, then mop up any mess:
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
FileInfo[] files;
try
{
files = dinfo.GetFiles("*.DOCX");
}
catch(DirectoryNotFoundException)
{
Console.WriteLine("ouch");
}
after all, any hardened code will need to catch this exception anyway, even if you believe that some microseconds ago the directory existed.
I think you can just do this:
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
if (!dinfo.Exists) // <---- check existence here
{
// your message here
}
else
{
// rest of your code here...
FileInfo[] Files = dinfo.GetFiles("*.DOCX");
foreach (FileInfo file in Files)
{
LB2.Items.Add(file.Name);
}
}

Read the contents of a listView, and display missing items in another listView based on the contents of a directory

I have a .NET webform that is displaying files found in a directory in a listView. This is the code for the display:
private void files()
{
try
{
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
listView1.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
label2.Text contains the directory that houses the files. What I need is for a second listView to display a list of documents housed in another directory to display if the file does not appear in the first list view.
The second directory contains templates where as the first directory contains completed documents. The names are different in each directory, but they are similar. For example a completed document displayed in the first listView may be called DEFECT1_AA09890.doc. It's template may be called 05DEFECT.doc.
It is easy enough to display the contents of the template directory using this code:
private void templateDocuments()
{
string path = #"\\directoryname\foldername";
try
{
DirectoryInfo dinfo = new DirectoryInfo(path);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
listView2.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
But this does not compare contents and display based on the results.
Long story short, I want to display the contents of a directory in a listView, compare it to the contents of another directory, and display in a second listView what does not appear in the first.
Any help would be much appreciated.
Cheers.
Before adding file names to listView2, you need to check whether you already added them to listView1. One way of doing that is to store the files in listView1 in a HashSet<string>, then checking that before adding to listView2. Something like this should work:
private void filesAndTemplates()
{
string path = #"\\directoryname\foldername";
HashSet<string> files = new HashSet<string>();
try
{
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
files.Add(file.Name);
listView1.Items.Add(file.Name);
}
dinfo = new DirectoryInfo(path);
Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
if (files.Contains(file.Name))
{
continue; // We already saw this file
}
listView2.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
EDIT
If you want inexact matching, you need to reduce the file name to its essence -- remove any decorations, which in your case looks to be one (or both) of
Leading digits
Underscore followed by whatever
The essence of 01hello_world.doc would thus be hello.
Regex should fit the bill quite nicely -- although the exact definition of the regular expression would depend on your exact requirements.
Define the Regex and a transformation method somewhere suitable:
private static readonly Regex regex = new Regex(
#"[0-9]*(?<core>[^_]+)(_{1}.*)?", RegexOptions.Compiled);
private static string Transform(string fileName)
{
int extension = fileName.LastIndexOf('.');
if (extension >= 0)
{
fileName = fileName.Substring(0, extension);
}
Match match = regex.Match(fileName);
if (match.Success)
{
return match.Groups["core"].Value;
}
return fileName;
}
Then modify the original method to transform the filename before adding files to the HashSet and before checking for their presence:
DirectoryInfo dinfo = new DirectoryInfo(label2.Text);
FileInfo[] Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
files.Add(Transform(file.Name)); // Here!
listView1.Items.Add(file.Name);
}
dinfo = new DirectoryInfo(path);
Files = dinfo.GetFiles("*.doc");
foreach (FileInfo file in Files)
{
if (files.Contains(Transform(file.Name))) // Here!
{
continue;
}
listView2.Items.Add(file.Name);
}
Note the two calls to Transform.

Copy a folder to Clipboard

I'm using C# as my programming language for this tryout.
I've searched through countless forums and other places that popped up on my Google search result. However, I cannot find a solution to my question.
I have a FileExplorer and I have the menu items Copy/Paste/Delete in my Context Menu Strip component. Now, I have Copy working for files in my File Explorer, but I'm trying to figure out how to copy folders.
I'm using the TreeView component as my primary component that this is tied to.
What is a File Explorer? Here is what I'm talking about (This is an actual image of my File Explorer):
Here is my current code for copying 'files' inside of my "FileExplorer\" folder. It also retrieves other folders/files inside of the 'FileExplorer\' folder.
private void toolStripMenuItemCopy_Click(object sender, EventArgs e)
{
try
{
DirectoryInfo[] directories = directoryInfo.GetDirectories();
foreach (FileInfo file in directoryInfo.GetFiles()) // Retrieving the files inside of FileExplorer\ folder
{
if (file.Exists && file.Name == treeView.SelectedNode.Text)
{
StringCollection filePath = new StringCollection();
filePath.Add(file.FullName);
Clipboard.SetFileDropList(filePath); // Copying the selected (node) file
}
}
if (directories.Length > 0)
{
foreach (DirectoryInfo directory in directories) // Retrieving the directories inside of the FileExplorer\ folder
{
foreach (FileInfo file in directory.GetFiles()) // Retreiving all the files inside of the directories
if (file.Exists && file.Name == treeView.SelectedNode.Text)
{
StringCollection filePath = new StringCollection();
filePath.Add(file.FullName);
Clipboard.SetFileDropList(filePath); // Copying the selected (node) file
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Help would be appreciated if someone could give me the needed pointers/code on how to Copy a folder inside of my File Explorer!
VB.NET
Dim f() As String = {"C:\SureFire\TWHomepage"}
Dim d As New DataObject(DataFormats.FileDrop, f)
Clipboard.SetDataObject(d, True)
StringCollection files = Clipboard.GetFileDropList();
foreach (string file in files)
{
if (System.IO.Directory.Exists(file))
{
string destPath = info.FullName;
FileSystem.CopyDirectory(file, destPath, UIOption.AllDialogs, UICancelOption.DoNothing);
}
}

To get selected list box value with folder path

I have a list box and its populated by this method,
private void ToReadFromExcel_Load(object sender, EventArgs e)
{
string folderpath = #"\\gibson\users";
// Call the method to show available files
PopulateListBox(ExcelListBox, folderpath, "*.csv");
}
// To populate list box with csv files from given path
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
String strItem;
foreach (Object selecteditem in ExcelListBox.SelectedItems)
{
strItem = selecteditem as String;
MessageBox.Show(strItem);
}
// read csv file information and insert into detail table
string filepath = #"\\gibson\users\CampManager.csv";
StreamReader sr = new StreamReader(filepath);
I hard coded the file path now, but I need to pass the filepath that was selected in the listbox. I have the file name in the variable stritem. If I want to pass the whole folder path how would I do that?
There is an ideal way. You should, instead of adding the FileInfo object's Name, should add the FileInfo object itself. So later you will be able to retrieve any piece of info related to that object, in your case say size, parent folder etc, and not just file name. Do it like this:
// To populate list box with csv files from given path
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file); //<-- note here
}
}
String strItem;
foreach (FileInfo selecteditem in ExcelListBox.SelectedItems)
{
StreamReader sr = new StreamReader(selecteditem.FullName);
//or whatever
}
One thing you should take care of here is to set the DisplayMember property of the ListBox like this:
ExcelListBox.DisplayMember = "Name";
What this does is to set the what property of the object in the listbox should be displayed. So here you choose FileInfo.Name which is what you want. This is how typically custom objects are added to ListBoxes in WinForms. You do not add just the string part of it typically. Akin to DisplayMember, there is also ValueMember property which is used to assign a value for each object, probably some id or so, but in your case nothing.
Few suggestions, 1) If you're using .NET 4, then use EnumerateFiles instead of GetFiles. The former is lazy and only yields a result when you start enumerating them (not beforehand), so it should be faster.
foreach (FileInfo file in dinfo.EnumerateFiles(FileType)) //<-- note here
{
lsb.Items.Add(file);
}
2) Use a using clause to dispose your stream reader properly, since that wont lock your file. It's always a good practice to make use of using. Its better on eyes than manual closing and disposing! Like this or so:
foreach (FileInfo selecteditem in ExcelListBox.SelectedItems)
{
using(StreamReader sr = new StreamReader(selecteditem.FullName))
{
//your code here
}
}

Categories

Resources