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);
Related
in this I am trying to load all the excel data from specified folderpath and its subfolder present inside it to sql server... Problem is I am able to read the main folder and subfolder, but while taking the data from sub folder file it taking the path of main folder for that sub folder file
example: file path - d:\test
sub directory : d:\test\test2
inside subdirectory my file is present but when iterating it is reading the path of sub directory file as : d:\test\file_name
please help in this
```DirectoryInfo directory = new DirectoryInfo(#FolderPath);
FileInfo[] files = directory.GetFiles("*.xlsx", SearchOption.AllDirectories);```
// Directory structure:
// C:\Temp\ExcelFiles\Excel1.xlsx
// C:\Temp\ExcelFiles\SubFolder\Excel2.xlsx
var path = #"C:\Temp\ExcelFiles";
var allExcelFilesInPathAndSubFolders = Directory.GetFiles(path, "*.xlsx", SearchOption.AllDirectories);
This approach returns the expected 2 Excel file names as string array (i.e. C:\Temp\ExcelFiles\Excel1.xlsx and C:\Temp\ExcelFiles\SubFolder\Excel2.xlsx).
The approach in your post returns an array of FileInfo. You can get all relevant information from that; the name with or without path, the file extension, and using Path.GetFileNameWithoutExtension you can get the name without extension.
// Directory structure:
// C:\Temp\ExcelFiles\Excel1.xlsx
// C:\Temp\ExcelFiles\SubFolder\Excel2.xlsx
var path = #"C:\Temp\ExcelFiles";
DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] files = directory.GetFiles("*.xlsx", SearchOption.AllDirectories);
// Excel1.xlsx
var firstFileName = files.First().Name;
// Excel1
var firstFileNameWithoutExtension = Path.GetFileNameWithoutExtension(files.First().Name);
// .xlsx
var fileFileExtension = files.First().Extension;
// C:\Temp\ExcelFiles\Excel1.xlsx
var firstFileFullName = files.First().FullName;
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
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);
}
When I want to save a file I use:
FileStream fs = new FileStream(fileName + ".asdf", FileMode.OpenOrCreateOrReadOrBlah);
which will save the file in:
C:\Users\ME\Documents\Visual Studio 2010\Projects\Project A\Project A\Project A\bin\x86\Debug
which is fine
but how do i list the files in that particular folder?
it will be different for every computer
List<string> fileNames = new List<string>();
DirectoryInfo di = new DirectoryInfo(****What goes here?****);
FileInfo[] rgFiles = di.GetFiles("*.asdf");
foreach (FileInfo fi in rgFiles)
{
fileNames.Add(fi.Name);
}
Thanks!
In your save, you are not specifying a directory, so it defaults to the current directory.
var di = new DirectoryInfo(Directory.GetCurrentDirectory());
First off, when you save a file without a path, it goes into the current working directory, not necessarily Debug.
Secondly, you can get the current working directory with:
string currentPath = Environment.CurrentDirectory;
DirectoryInfo di = new DirectoryInfo(****What goes here?****);
"Here" is where the directory name which you want to manipulate would go.
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);
}