i have a problem where if i get my files from folder (gedaan)
it duplicates and put the files from the folders in my listbox agian.
i just want it to check if there are new files in my folder
this is my code to get files from a folder
DirectoryInfo dinfo = new
DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
FileInfo[] Files = dinfo.GetFiles("*.DOCX");
foreach (FileInfo file in Files)
{
LB2.Items.Add(file.Name);
}
It is because you are adding all files again.
You either have to clear your collection before adding all files again:
DirectoryInfo dinfo = new
DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
FileInfo[] Files = dinfo.GetFiles("*.DOCX");
LB2.Items.Clear();
foreach (FileInfo file in Files)
{
LB2.Items.Add(file.Name);
}
Or you have to exclude duplicate files:
DirectoryInfo dinfo = new
DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
IEnumerable<FileInfo> Files = dinfo.GetFiles("*.DOCX").Where(file => !LB2.Items.Contains(file.Name));
foreach (FileInfo file in Files)
{
LB2.Items.Add(file.Name);
}
Another approach would be to change the type of LB2.Items to the type HashSet<string>. HashSet<T> is is a collection that contains no duplicate elements. Read more here: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1
Related
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);
}
}
I am trying to recursively parse a directory ,look for .tar.bz2 files in the subdirectories , untar and create a list of all the files inside the tar file,following is the detailed info,how to accomplish this?
path = \\location\tarballfiles\
lets say the following .tar.bz2 files are present inside "path" in the mentioned locations
RA900B\hw.1\cqq-tech-fw-RA900B_hw_2-DATA.3.4-00028-S-1.tar.bz2
CQQ9888\hw.2\cqq-tech-fw-CQQ9888_hw_2-DATA.3.4-00028-S-1.tar.bz2
......
EXPECTED OUTPUT:-
RA900B\hw.1\top.bin
RA900B\hw.1\mtp.bin
CQQ9888\hw.2\test.txt
CQQ9888\hw.2\data.txt
.......
PSEUDO CODE:-
for each subfolder under path(ignore any files)
parse recursively to look for .tar.bz2 files
untar
create a list of all the files
To find your tarball files in a directory, you can use:
string[] files = Directory.GetFiles("\\\\location\\tarballfiles\\", "*.tar.bz2", SearchOption.AllDirectories);
and then recurse the files, open them, untar then and add the files of the tar to a list.
Example (with some pseudo code):
var allFiles = new List<string>();
string[] files = Directory.GetFiles("\\\\location\\tarballfiles\\", "*.tar.bz2", SearchOption.AllDirectories);
foreach (var file in files)
{
// untar
var filesFromTar = file.untar();
foreach (var fileNameFromTar in filesFromTar)
{
allFiles.Add(fileNameFromTar);
}
}
foreach (var allFile in allFiles)
{
Console.WriteLine(allFile);
}
How can I list the text files in a certain directory (C:\Users\Ece\Documents\Testings) in a listbox of a WinForm(Windows application)?
// What directory are the files in?...
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestDirectory");
// What type of file do we want?...
FileInfo[] Files = dinfo.GetFiles("*.txt");
// Iterate through each file, displaying only the name inside the listbox...
foreach( FileInfo file in Files )
{
listbox1.Items.Add(file.Name);
}
// A statement, followed by a smiley face...
That oughta do it. ;o)
To get the txt files, try this:
var folder = #"C:\Users\Ece\Documents\Testings";
var txtFiles = Directory.GetFiles(folder, "*.txt");
listBox.Items.AddRange(txtFiles);
I have a web directory that contains files and I want to delete them all. I've looked online but all the answers rely on the file system and I want to use the website's directories. I tried this:
foreach (string file in HttpContext.Current.Server.MapPath("\\MyDirectory"))
{
File.Delete(file);
}
The foreach statement is underlined and the error is 'cannot convert type char to string'.
What's the syntax to delete all files in a directory?
Your might need to correct your MapPath parameter (\\MyDirectory), but the syntax you need is shown below.
System.IO.DirectoryInfo di= new DirectoryInfo(HttpContext.Current.Server.MapPath("\\MyDirectory"));
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
Server.MapPath gives you a directory path, not an array of files/folders. IF you want to remove all files in the folder you would do this:
var folderPath = HttpContext.Current.Server.MapPath("\\MyDirectory");
foreach (string file in Directory.GetFiles(folderPath))
{
File.Delete(file);
}
If you wanted do delete the folder, then
var folderPath = HttpContext.Current.Server.MapPath("\\MyDirectory");
Directory.Delete(folderPath);
To delete all folders within the main folder
var folderPath = HttpContext.Current.Server.MapPath("\\MyDirectory");
foreach (string file in Directory.GetDirectoriesfolderPath))
{
File.Delete(file);
}
writing to a text file is working good...But my problem is text file is created in different path....
Here is my code...
var _logFolderPath = Path.Combine(textboxPath.Text.Trim(), "log");
string[] subdirectoryEntries = Directory.GetDirectories(Path.Combine(txtBoxInput.Text, "RM"));
foreach (string subdirectory in subdirectoryEntries)
{
string[] x = Directory.GetFiles(subdirectory);
DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
............
}
and
using (var dest = File.AppendText(Path.Combine(_logFolderPath,subdirectory+ ".txt")))
Here i have to store the subdirectory named text file in _logFolderPath path....But the text files are generared in the subdirectory path...I just need the name of the subdirectory to be created as a text file in _logFolderPath path...How to get the name of the subdirectory alone....Any suggestion?
Have a look at using DirectoryInfo and DirectoryInfo.Name Property
You should use DirectoryInfo instead. It can give you just the directory name. Here is a sample
DirectoryInfo root = new DirectoryInfo("C:\\");
foreach (DirectoryInfo s in root.GetDirectories())
{
Console.Out.WriteLine(s.Name);
}