Reputation: 19
I have a problem and I think you can help me.
I want to compare two directories the following code works but I have a problem to show the result. I want to make a list of steps that show how to make the structure of both directories show as same.
I have this code and I dont know how to add the folder name from directory 1 + the Filenames:
System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);
// Take a snapshot of the file system.
IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
foreach (var v in queryList1Only)
{
listbox_2f.Items.Add("Create: "+ dir2.FullName+ "\\" + v.Name);
}
How I can solve this problem that I can add the foldernames from directory 1 to listbox.
Ok i will give you an example expect I have a directory like: C:\Users\User\Desktop\Test1 in this directory are subdirectories and files now I have to show a list how to make the structure of these directory same as of C:\Users\Jonas\Desktop\Test2 so when I have in Test1 in example the folders Test12 and in these a text123.txt file with the path: C:\Users\User\Desktop\Test1\Test12\text123.txt who could I add in the listbox something like "Create: C:\Users\User\Desktop\Test2\Test12\text123.txt" Of course the path isn`t everytime the same
Upvotes: 0
Views: 123
Reputation: 7918
In order to extract the folder (in other word, directory) name from the path, you can use Path.GetDirectoryName()
method as detailed in: http://msdn.microsoft.com/en-us/library/system.io.path_methods%28v=vs.110%29.aspx
Also, refer to the C# usage example given in: http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname%28v=vs.110%29.aspx
string filePath = @"C:\MyDir\MySubDir\myfile.ext";
string directoryName;
int i = 0;
while (filePath != null)
{
directoryName = Path.GetDirectoryName(filePath);
Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", filePath, directoryName);
filePath = directoryName;
if (i == 1)
{
filePath = directoryName + @"\"; // this will preserve the previous path
}
i++;
}
/*
This code produces the following output:
GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir'
GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir\') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir') returns 'C:\'
GetDirectoryName('C:\') returns ''
*/
Regards,
Upvotes: 0