I am making a program in which users can modify remote files. I put the selected files (depending on some predefined criteria) in a listView, but I display only the file names, not full filepaths.
The problem I get however, is that when a user would double-click on an item, it should open another window to modify that item.
private void listView1_DoubleClick(object sender, EventArgs e)
{
account = File.ReadAllLines("\\\\myremoteserver\\ftp\\"+listView1.SelectedItems[0].Text+".txt");
Form3 passForm = new Form3();
passForm.ShowDialog();
}
private void Form2_Load(object sender, EventArgs e)
{
string[] files = Directory.GetFiles("\\\\myremotserver\\ftp\\","*.txt", System.IO.SearchOption.AllDirectories);
foreach (string s in files)
{
listView1.Items.Add(Path.GetFileNameWithoutExtension(s));
}
}
The problem is, that the files are all in different subfolders, so if I leave the code as is, it will not display the correct content of the file. For example, the file is called test1.txt, it is placed in myremoteserver\ftp\testfolder\test1.txt, but with my program, it will try to find the file in myremoteserver\ftp\test1.txt.
What I am asking is, if it is possible to modify the listView in such a way, that the full file path is always saved, but only the file names are displayed? I do not want the user to see the complete file path of the files, just the file names.
Use the Tag property of the ListViewItem
So to create items...
foreach (string s in files)
{
ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(s));
lvi.Tag = s;
listView1.Items.Add(lvi);
}
Then in event handler...
account = File.ReadAllLines("\\\\myremoteserver\\ftp\\"+listView1.SelectedItems[0].Tag +".txt);
Related
I failed to fill combobox with csv filenames. I created the combobox by dragging from toolbox in Microsoft Visual Studio. I set the name of combobox to ChooseSampleSheet.
The following is my code:
private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo d = new DirectoryInfo(#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
ChooseSampleSheet.DataSource = Files;
ChooseSampleSheet.DisplayMember = "Name";
}
Also, I tried the following code:
private string path = (#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");
private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> Configurations = Directory.EnumerateDirectories(path, "*.exe")
.Select(p => Path.GetFileName(p))
.ToList();
ChooseSampleSheet.DataSource = Configurations;
}
But Neither of them works. Nothing shows in my combobox. I expected to see csv file names. So that I can click to open selected file afterwards (not show in in my code).
People suggested me to change the event. The following is my update.
private void form4_load(object sender, EventArgs e)
{
DirectoryInfo d = new DirectoryInfo(#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
ChooseSampleSheet.DataSource = Files;
ChooseSampleSheet.DisplayMember = "Name";
}
private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
}
However, nothing show in the combobox still.
I do not see anything wrong in your code however I think your code is at the wrong place.
SelectedIndexChanged will get execute when you select something from the drop down. Since your drop down has not been filled with values , you can't fire that event.
Put the same code in form_load and you will see the values there.
DirectoryInfo d = new DirectoryInfo(#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
ChooseSampleSheet.DataSource = Files;
ChooseSampleSheet.DisplayMember = "Name";
I want to reduce the length of the path and display only the file name.
For example, suppose the path is c:\\program files\...\123.jpg I want to display only 123.jpg.
Here is the code I have been working with thus far. Can anyone suggest modifications?
private void button1_Click(object sender, EventArgs e)
{
panel3.Controls.Clear();
var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "DICOM Files (*.dcm;*.dic)|*.dcm;*.dic|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.Cancel)
return;
foreach (string s in ofd.FileNames)
{
listBox1.Items.Add(s);
}
}
There is a class named Path in System.IO namespace.
Between its numerous static methods you could find
Path.GetFilename(string);
Using it in your Add loop you could set only the filenames
listBox1.Items.Add(Path.GetFileName(s));
However, I suggest to save the folder name somewhere, because if you need to process these files you need it. And, guess what, Path has a method also to extract a path from a full filename
if(filenames.Length > 0)
string workingPath = Path.GetDirectoryName(filenames[0]);
EDIT
From your comments below it seems that you call this button_click more than one time and every time you select a different folder. In this case, stripping the path part from the selected filenames leaves your listbox filled with files that you can't retrieve because you don't know the path part (stripped away). If you need to retrieve the files selected to execute some kind of process, then you need to store somewhere the full path of these files and be able to retrive them.
You can achieve this result storing the files selected in a List<string> instance.
Declare at the global level a variable to store these full filenames
(Add using System.Collection.Generic;)
List<string> selectedFiles = new List<string>();
Now inside the button click add the full filename to the List<string> and the stripped file to the ListBox items, in the same order
private void button1_Click(object sender, EventArgs e)
{
panel3.Controls.Clear();
var ofd = new OpenFileDialog();
ofd.Multiselect = true;
ofd.Filter = "DICOM Files (*.dcm;*.dic)|*.dcm;*.dic|All Files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.Cancel)
return;
foreach (string s in ofd.FileNames)
{
listBox1.Items.Add(Path.GetFileName(s));
selectedFiles.Add(s);
}
}
Now, if you want to retrieve the full path for the selected file in the listbox you could use
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if(listBox1.SelectedIndex >= 0)
{
string fullFileName = selectedFiles[listBox1.SelectedIndex];
.... process the filename ....
}
}
Use Path.GetFileName() to return the name
see http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx
I'm a newbie in C# and I have 2 Listboxes l-->istBox1 and listBox2 and I want to load files from folder into these listboxes.
I tried like this :
listBox1:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.rtdl");
foreach (FileInfo file in Files)
{
listbox1.Items.Add(file.Name);
}
}
listBox2:
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.dlz");
foreach (FileInfo file in Files)
{
listbox2.Items.Add(file.Name);
}
}
when i run the form, the files from the folder is not displaying???
Instead of listBox1_SelectedIndexChanged, update the listbox against some button click, otherwise your code looks fine. Initially you probably don't have any item in your listbox and that's why SelectedIndexChanged doesn't get fired when you click on it.
Edit: (Since the question has been edited, I will update my answer)
To pouplate your listboxes with Files, you should do that, in some event other than SelectedIndexChanged. Because at the start of your application your listboxes are empty and SelectedIndexChanged event gets fired when there are items in the listbox and user click on it. You may create the following function
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
Now you may call this function with your listbox in some event against a button click or form load. e.g.
private void Form1_Load(object sender, EventArgs e)
{
PopulateListBox(listbox1, #"C:\TestLoadFiles", "*.rtld");
PopulateListBox(listbox2, #"C:\TestLoadFiles", "*.other");
}
This might work ;)
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.rtdl");
foreach (FileInfo file in Files)
{
listbox2.Items.Add(file.Name);
}
}
Wrong event i suppose. Move that code to the constructor of your form/control or attach it to an event of another control. Repopulating the listBox on SelectedIndexChanged when the initial state of the listbox is empty does not make sense.
I have a form with a text box for a user to enter a search string for folder names.
The app then finds matching folder(s) on the network.
If a single folder is returned it opens in explorer.
If multiple folders are returned in the search they are added to an array as a unc path.
I need to know the best way or which object to use to populate the contents of the array with on the main form.
I then need to be able to double click on the desired result to open the containing folder in explorer to handle multiple matches.
searching for match1
array could contain something like: H:\match1, G:\Match1, K:\folder1\Match1
If I understood your intentions correctly, a simple ListBox should do. You can handle each path in your array as one list entry and listen to the double click to open the explorer window.
To fill the listbox you could utilize the datasource attribute:
string[] paths = new string...
// fill array
yourListBox.DataSource = paths;
Addendum: To react to the double click simply listen to the double click event of the list box and in the event handler, do something like this:
private void yourListBox_DoubleClick(object sender, EventArgs e)
{
openExplorerWindow((string)yourListBox.SelectedItem);
}
public Form2()
{
InitializeComponent();
ArrayList paths = new ArrayList();
paths.Add("path1");
paths.Add("path2");
paths.Add("path3");
paths.Add("path4");
foreach (string test in paths)
{
listBox1.Items.Add(test);
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
string path = listBox1.SelectedItem.ToString();
}
I'm not sure whether this topics has been disscussed before or not, but I'm not sure the exact word to search for it. What method/class should I use?
The program has 3 buttons: 1) for folder browsing, 2) scan for the selected folder content, and 3) open the file. When user browse the selected folder**(1), user click scan button to scan from the first file until the last available files and listed it text box(2)** and from that user can decide whether to open the files or not**(3)**.
Here are what have I done so far (no 1 and 3):
//For browse.
private void browse2()
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
this.txtDest.Text = folderBrowserDialog1.SelectedPath;
}
}
//For opening folder.
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
Process.Start(txtDest.Text);
}
catch
{
MessageBox.Show("Please select one file/folder");
}
}
If you are trying to just open a file you can directly use an Open File Dialog.
If you need to display the contents of a directory you can use the Directory Info Class.
Well my example is a WPF app that adds files/ folders in a directory to a treeview, but you should get the general idea:
Note: Code was written for a training exercise, and therefore only goes 3 levels deep, as a proof of concept kind of thing
private void Window_Loaded(object sender, RoutedEventArgs e)
{
foreach (DriveInfo di in DriveInfo.GetDrives())
{
TreeViewItem drive = new TreeViewItem();
drive.Header = di.Name;
treeView1.Items.Add(drive);
DirectoryInfo folders = new DirectoryInfo(di.Name);
// The depth count means that it only goes 3 levels deep, to make it quick to load
GetFoldersAndFiles(drive, folders, 3);
}
}
private static void GetFoldersAndFiles(TreeViewItem parent, DirectoryInfo folders, int depth)
{
if ((depth > 0)
{
foreach (DirectoryInfo dirI in folders.GetDirectories())
{
TreeViewItem dir = new TreeViewItem();
dir.Header = dirI.Name;
parent.Items.Add(dir);
GetFoldersAndFiles(dir, dirI, depth - 1);
}
foreach (FileInfo fileI in folders.GetFiles())
{
TreeViewItem file = new TreeViewItem();
file.Header = fileI.Name;
parent.Items.Add(file);
}
}
}