Files without extensions in listbox - c#

1.) How I can remove file extension (".txt")?
2.) Why when I click somewhere to listbox window so all items are replicated???
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Folder");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
}
}

listBox.Items.Clear(); // to clear current listbox items
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
listBox1.Items.Add(Path.GetFileNameWithoutExtension(file.Name));
}

Related

How do I list the file in it by increasing the filename by one?

I have thousands of project folders and inside each folder I have another folder named test. There is only one pdf file in this folder. When I select the top folder, I want to get the pdfs in these folders and copy them to another location. How can I select these pdfs?
There are other pdfs under the Project Folders, I only want the pdfs in the test folder.
My folder names are increasing one by one(Project1,Project2,3,4,5,6,7.....)
My Project
 ↳-Project1
     ↳Test
        ↳Project1.pdf
 ↳-Project2
     ↳Test
        ↳Project2.pdf
 ↳-Project3
     ↳Test
        ↳Project2.pdf
I want to something just like this
The image belongs to me. Currently it only lists all pdfs in selected folders. I couldn't filter pdf files. I am using FolderBrowserDialog for select the folder
private string[] dirs;
private string[] files;
private System.IO.FileInfo file;
private void GetPDF(string path, ref List<string> listPDF)
{
try
{
dirs = System.IO.Directory.GetDirectories(path);
foreach (string item in dirs)
{
GetPDF(item, ref listPDF);
}
files = System.IO.Directory.GetFiles(path);
foreach (string item in files)
{
file = new System.IO.FileInfo(item);
if (file.DirectoryName.ToLower().EndsWith("test") && file.Extension.ToLower() == ".pdf")
{
listPDF.Add(file.FullName);
}
}
}
catch (Exception)
{
throw;
}
}
private void Function1()
{
try
{
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
List<string> listPDF = new List<string>();
GetPDF(dialog.SelectedPath, ref listPDF);
listPDF.Sort();
foreach(string filePath in listPDF)
{
Console.WriteLine(filePath);
}
}
}
catch (Exception)
{
throw;
}
}

Filling Combo/ListBox with file names from a directory

I'm attempting to populate a ListBox with file names from a directory. My code works, however, when I recompile the program the items are no longer there. Also when I click on an item in the ListBox the content of the ListBox is duplicated over and over. Any guidance would be much appreciated, thanks.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(".\\Notes\\");
FileInfo[] files = dir.GetFiles("*.txt");
foreach ( FileInfo file in files )
{
listBox1.Items.Add(file);
}
}
You've populated your ListBox in the incorrect event. So each time you select an item, the ListBox is populated again. You should put it in another event like Button_Click or Form_load:
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(".\\Notes\\");
FileInfo[] files = dir.GetFiles("*.txt");
foreach ( FileInfo file in files )
{
listBox1.Items.Add(file);
}
}
//Or in a Button_Click event
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(".\\Notes\\");
....
}
So thanks to the advice of #S.Akbari my solution to my issue is below.
public Form1()
{
InitializeComponent();
DirectoryInfo dir = new DirectoryInfo(".\\Notes\\");
FileInfo[] files = dir.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file);
}
}

Show file names in listbox for selected directory in treeview

I have a button "Choose folder". When i choose folder, it shows all the directories and sub-directories in treeview. Everything is working fine.
What i need now is - when i click in the treeview on some directory in shows all files that is in that directory in the listbox.
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
DirectoryInfo directoryInfo = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
if (directoryInfo.Exists)
{
VeidotKoku(directoryInfo, treeView1.Nodes);
}
}
}
private void VeidotKoku(DirectoryInfo directoryInfo, TreeNodeCollection Pievienot)
{
TreeNode tagadejaNode = Pievienot.Add(directoryInfo.Name);
foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
{
VeidotKoku(subdir, tagadejaNode.Nodes);
}
}
System.IO.Path.GetFileNameWithoutExtension(fileLocationPath);
Will get the extensionless path.
Else, if you just want to get the value you can split the path and get the last element.
foreach(string filePath in filePaths)
{
string[] brokenPath = filePath.Split('/');
listBox.Add(brokenPath.Last());
}
Top of my head.

Removing Info Before Filename.exe

This lists all files in all directory's from the directory i choose with folderBrowserDialog1, but when it loads them into the listBox it comes up with the item in the listBox like this
C:\users\username\desktop\filename.exe
C:\users\username\desktop\filename.exe
C:\users\username\desktop\filename.exe
and so on.. is there any way to remove C:\users\username\desktop\ and just keep filename.exe
Here's my code it may help.
private void DirSearch(string dir)
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
foreach (var file in files)
{
ListBox2.Items.Add(file);
}
}
Use Path.GetFileName method:
ListBox2.Items.Add(Path.GetFileName(file));
From your comment to #Dennis, this should work.
private void DirSearch(string dir)
{
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
foreach (var file in files)
{
ListBox2.Items.Add(file.Replace(dir, string.empty);
}
}
try recursive method
private void Form1_Load(object sender, EventArgs e)
{
DirSearch(folderBrowserDialog1.SelectedPath);
}
private void DirSearch(string dir)
{
try
{
string userpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
folderBrowserDialog1.ShowDialog();
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
if (!dir.Equals(userpath))
{
foreach (var file in files)
{
listBox1.Items.Add(System.IO.Path.GetFileName(file));
}
IEnumerable<string> dirs = Directory.EnumerateDirectories(dir);
foreach (string dsdir in dirs)
{
DirSearch(dsdir);
}
}
}
catch (Exception ex)
{
}
}

How to add files recursively to a listbox by selecting a dir in C#

I'm trying to add a list of files to a listbox which follows all sub-directories
Currently I am using the following code however it only searches one directory and does not recurse into subdirectories.
FolderBrowserDialog odd = new FolderBrowserDialog();
private void button2_Click(object sender, EventArgs e)
{
if (odd.ShowDialog() == DialogResult.OK)
{
string folderName = odd.SelectedPath;
foreach (string f in Directory.GetFiles(folderName))
checkedListBox1.Items.Add(f);
}
}
Now I modified to this, but says string f doesn't exist in the current code
foreach (string f in Directory.GetFiles(folderName, "*.*", SearchOption.AllDirectories));
checkedListBox1.Items.Add(f);
You could use
string[] filePaths = Directory.GetFiles(#"C:\CurrentDirectoryName", "*.*", SearchOption.AllDirectories);
Then bind the listbox to that string array

Categories