How can I load a folders files into a ListView? - c#

I'd like to have a user select a folder with the FolderBrowserDialog and have the files loaded into the ListView.
My intention is to make a little playlist of sorts so I have to modify a couple of properties of the ListView control I'm assuming. What properties should I set on the control?
How can I achive this?

Surely you just need to do the following:
FolderBrowserDialog folderPicker = new FolderBrowserDialog();
if (folderPicker.ShowDialog() == DialogResult.OK)
{
ListView1.Items.Clear();
string[] files = Directory.GetFiles(folderPicker.SelectedPath);
foreach (string file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
ListViewItem item = new ListViewItem(fileName);
item.Tag = file;
ListView1.Items.Add(item);
}
}
Then to get the file out again, do the following on a button press or another event:
if (ListView1.SelectedItems.Count > 0)
{
ListViewItem selected = ListView1.SelectedItems[0];
string selectedFilePath = selected.Tag.ToString();
PlayYourFile(selectedFilePath);
}
else
{
// Show a message
}
For best viewing, set your ListView to Details Mode:
ListView1.View = View.Details;

A basic function could look like this:
public void DisplayFolder ( string folderPath )
{
string[ ] files = System.IO.Directory.GetFiles( folderPath );
for ( int x = 0 ; x < files.Length ; x++ )
{
lvFiles.Items.Add( files[x]);
}
}

List item
private void buttonOK_Click_1(object sender, EventArgs e)
{
DirectoryInfo FileNm = new DirectoryInfo(Application.StartupPath);
var filename = FileNm.GetFiles("CONFIG_*.csv");
//Filename CONFIG_123.csv,CONFIG_abc.csv,etc
foreach(FileInfo f in filename)
listViewFileNames.Items.Add(f.ToString());
}

Related

webBrowser control not displaying PDF

I have problem in displaying pdf image in the webBrowser control. The file name is search in the web browser.
refer to image below:
Code:
FolderBrowserDialog FBD = new FolderBrowserDialog();
if (FBD.ShowDialog() == DialogResult.OK)
{
//textBox5.Text = new DirectoryInfo(FBD.SelectedPath).Parent.Parent.Name;
string[] files = Directory.GetFiles(FBD.SelectedPath, "*.pdf");
// string[] dirs = Directory.GetDirectories(FBD.SelectedPath);
foreach (string file in files)
{
listBox1.Items.Add(new FileInfo(file).Name);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string filedirectory = listBox1.SelectedItem.ToString();
// if (listBox1.SelectedItem != null && listBox1.SelectedItem is string)
{
webBrowser1.Navigate(filedirectory);
// if (this.listBox1.SelectedIndex >= 0)
Instead of this:
string[] files = Directory.GetFiles(FBD.SelectedPath, "*.pdf");
foreach (string file in files)
{
listBox1.Items.Add(new FileInfo(file).Name);
}
do this:
var folder = new DirectoryInfo(FBD.SelectedPath);
var files = folder.GetFiles("*.pdf");
listBox1.DisplayMember = nameOf(FileInfo.Name);
listBox1.ValueMember = nameOf(FileInfo.FullName);
listBox1.DataSource = files;
and then, instead of this:
string filedirectory = listBox1.SelectedItem.ToString();
do this:
var filePath = (string)listBox1.SelectedValue;
Note the use of a variable name that actually describes what the variable represents. "filedirectory" is a nonsense.

How to get Full Path Of Listbox Item

I want to get Full Path Of Listbox item, because I want to showing all item of ImageListView , Can you give me some idea , If I add A Folder, All Items Of A will show in ImageList View Then it could save setting , Again I add B Folder , All Items Of B will show in ImageList View and save setting.
Thanks in Advance !!
BindingSource listboxsource = null;
public Form1()
{
InitializeComponent();
listboxsource = new BindingSource(Properties.Settings.Default.listboxitems, "");
someListbox.DataSource = listboxsource;
}
//Add Folder in Listbox
private void button57_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = "Please Select Folder";
if (dialog.ShowDialog() == DialogResult.OK)
{
string folderName = dialog.SelectedPath;
string s = Path.GetFileName(folderName);
listboxsource.Add(s);
Properties.Settings.Default.Save();
listboxsource = new BindingSource(Properties.Settings.Default.listboxitems, "");
someListbox.DataSource = listboxsource;
}
}

Get and return filepath by foreach

I want to make a program to able to save every file path which the user selected.
after that do some prosses for each file. for example, convert video file one by one.
Could you tell me why foreach does not work?
private void btnInput_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialogInput = new OpenFileDialog();
openFileDialogInput.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
openFileDialogInput.Filter = "Video Files|*.mp4|TS Files|*.ts";
openFileDialogInput.Multiselect = true;
openFileDialogInput.FilterIndex = 1;
DialogResult result = openFileDialogInput.ShowDialog();
string [] inputPath = openFileDialogInput.FileNames;
foreach (var item in inputPath)
{
item;
}
}
inputPath gets all file paths that the user selected. but I don't know how can I get them, one by one and make some prosses on them.
You Can try this:
private void AddWatermark(string videoFilePath)
{
// Add your logic here to add watermark
}
And in the foreach loop:
foreach (var item in inputPath)
{
AddWatermark(item);
}

Deleting .png File through ListView

I am making an application for personal use that will organize .png files and will let me remotely delete them from the directory through the application (through a ListView).
I have a snippet that will delete the file from the ListView, but not from the actual file directory. I want to be able to do both when I click delete.
private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("This will delete the file from the folder. Are you sure?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
for (int i = fileDisplayListView.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem item = fileDisplayListView.SelectedItems[i];
fileDisplayListView.Items[item.Index].Remove();
File.Delete(fbd.SelectedPath + fileDisplayListView.Items.ToString());
}
}
Additional snippet for more information..
private void openToolStripButton_Click(object sender, EventArgs e)
{
fbd.ShowDialog();
DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
directoryPath.Text = "Directory: " + fbd.SelectedPath;
FileInfo[] Files =
di.GetFiles("*.PNG*", SearchOption.AllDirectories);
if (Files.Length == 0)
MessageBox.Show("No .png files found in directory...", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Question);
fileDisplayListView.Items.Clear();
foreach (FileInfo f in Files)
{
ListViewItem item = new ListViewItem(f.Name);
this.fileDisplayListView.Items.Add(f.Name);
}
this.fileDisplayListView.View = View.Details;
this.fileDisplayListView.Refresh();
}
The last part of it, File.Delete(fbd.SelectedPath + fileDisplayListView.Items.ToString());
is not functional. Please help!
This code gets a list of all .jpg files in the directory, adds them to the ListView. By pressing the button it deletes the selected ListView elements and files:
private FileInfo[] files;
public Form1()
{
InitializeComponent();
files = new DirectoryInfo(#"C:\Users\User\Pictures").GetFiles("*.jpg", SearchOption.AllDirectories);
foreach (var file in files)
{
listView1.Items.Add(file.Name);
}
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
var curentItem = listView1.SelectedItems[i];
foreach (FileInfo file in files)
{
if (curentItem.Text == file.Name)
{
listView1.Items.Remove(curentItem);
file.Delete();
i--;
}
}
}
}
This is the snippet I used to fix the application. It is a lot more directly related to how I am approaching the problem.
if (MessageBox.Show("This will delete the file from the folder. Are you sure?", "Warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
for (int i = fileDisplayListView.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem item = fileDisplayListView.SelectedItems[i];
string fpath = string.Empty;
fileDisplayListView.Items[item.Index].Remove();
fpath = fbd.SelectedPath.ToString() + "\\" + item.Text;
File.Delete(fpath);
}

listbox drag and drop items, items name

I have some function
private void listBox2_Drop(object sender, System.Windows.DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop, false);
for (int i = 0; i < files.Length; i++)
{
ListItemEl el = new ListItemEl();
el.ui = files[i];
listBox2.Items.Add(el);
}
}
the row:
(string[])e.Data.GetData(System.Windows.DataFormats.FileDrop, false)
return file location.
Which function returns me from DragEventArgs name of file?
You already have the full path just get the filename with Path.GetFileName
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var filename in files)
{
var nameOnly = System.IO.Path.GetFileName(filename);
}

Categories