Code:
private void loadViewTemplates(string path)
{
foreach (string file in Directory.GetFiles(path, "*.txt"))
{
ToolStripItem subItem = new ToolStripMenuItem();
viewTemplatesToolStripMenuItem.DropDownItems.Add(subItem);
}
}
I have three files in the source directory, they seem to appear as the menu subitem, but the file names did not appear.
Is there a way I can make the file names' appear instead of invisible? Your help would be much appreciated. Thank you!
Missing the
subItem.Text = Path.GetFileNameWithoutExtension(file);
From MSDN
ToolStripItem.Text - Gets or sets the text that is to be displayed on the item.
So the code will be
private void loadViewTemplates(string path)
{
foreach (string file in Directory.GetFiles(path, "*.txt"))
{
ToolStripItem subItem = new ToolStripMenuItem();
subItem.Text = Path.GetFileNameWithoutExtension(file);
viewTemplatesToolStripMenuItem.DropDownItems.Add(subItem);
}
}
I have found a solution myself as below:
private void loadViewTemplates(string path)
{
foreach (string file in Directory.GetFiles(path, "*.txt"))
{
viewTemplatesToolStripMenuItem.DropDownItems.Add(Path.GetFileNameWithoutExtension(file));
}
}
Thank you.
Related
Im a beginner programmer and im stuck on this project for my internship
Lets say i have hello.txt in folder1 Listbox1 takes it from folder1 and put it in the listbox.
listbox2 does the same thing with folder2 except with a diffrent extension
After for example i have created hello.DOCX i need hello.txt to be removed from listbox1 but not from folder1
i hope this is clear
this is my code to get files from folders
private void LBNietGedaan_Loaded(object sender, RoutedEventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\nour\Desktop\Niet gedaan");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
LB1.Items.Add(file.Name);
}
}
private void LBGedaan_Loaded(object sender, RoutedEventArgs e)
{
//zet files van een folder in de listbox
DirectoryInfo dinfo = new
DirectoryInfo(#"C:\Users\nour\Desktop\Gedaan");
FileInfo[] Files = dinfo.GetFiles("*.DOCX");
foreach (FileInfo file in Files)
{
LB2.Items.Add(file.Name);
}
}
Can't you just make a property with an ObservableCollection or so and then bind the listbox to it? Then you can simply clear it and the listbox will be empty again.
Binding ObservableCollection to WPF ListBox
This is pseudo, so it might need some adjusting, but basically you can but it in a button click, or place it in your first method after you load the list, adjust the variables to match yours.
List<string> one = new List<string>();
foreach (String string1 in LB1.Items)
{
one.Add(string1);
}
List<string> two = new List<string>();
foreach (String string2 in LB2.Items)
{
two.Add(string2);
}
foreach (String string1 in one)
{
foreach (String string2 in two)
{
string cat1 = string1.Substring(0, string1.Length - 4);
string cat2 = string2.Substring(0, string2.Length - 5);
if (cat1.Equals(cat2))
{
LB1.Items.Remove(string1);
// if you want to stop after the first match, break;
// else remove break to find all matches;
break;
}
}
}
Basically this will search the items in listbox1's Items, and remove any file called "yourtarget.txt". I think should send you in the right direction. If you need more let me know!
I have a ListView that loads a .txt file with some phone numbers and when i click the delete button it will delete the selected line from the ListView. When I click the button to delete, the button delete the line but doesn't save the file with the updated list.I'll provide more information if asked.
Thank you!!
here is the delete button
public void Deletar_lista()
{
var caminho = Directory.GetCurrentDirectory();
caminho += "\\telefones.txt";
listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
}
I only have a little bit of code to go on here, but it appears that you are only deleting the item from the ListView.
Your application will not automatically know that you want it to save the updated content of the ListView to the file.
To do this, try the following:
string[] lines = System.IO.File.ReadAllLines(caminho);
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(caminho))
{
foreach (string line in lines)
{
if (!line.Contains(listView1.SelectedIndices[0].ToString()))
{
writer.WriteLine(line);
}
}
}
This reads all the lines from the file, and if the selected line equals your deleted entry, it will not write it to the file.
I manage to make it work. Here is what I done:
int ll_count = -1;
var caminho = Directory.GetCurrentDirectory();
caminho += "\\telefones.txt";
if (MessageBox.Show("Deseja mesmo deletar o numero?", "Usuario", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
string[] lines = File.ReadAllLines(caminho);
using (StreamWriter writer = new StreamWriter(caminho))
{
foreach (string line in lines)
{
ll_count++;
if (ll_count != listView1.SelectedIndices[0])
{
writer.WriteLine(line);
}
}
}
listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
I have created a comboBox inside a Windows Form and inside this comboBox I want to show filenames inside a specific directory.
My code Form 1:
private string path = (#"C:\Users\khaab\Documents\visual studio 2015\Projects\ReadingXML\ReadingXML\bin\Debug\Customers");
private void SelectConfigComboBox_DropDown(object sender, EventArgs e)
{
List<String> Configurations = Directory.EnumerateDirectories(path).ToList();
Path.GetFileName(path);
SelectConfigComboBox.DataSource = Configurations;
}
My problem at this moment is that when I click on the ComboBox it shows me the whole path name I want only the names of the file inside that directory.
after enumerating all files apply Path.GetFileName method to each of them using Select extension method:
private void SelectConfigComboBox_DropDown(object sender, EventArgs e)
{
List<String> Configurations = Directory.EnumerateFiles(path)
.Select(p => Path.GetFileName(p))
.ToList();
SelectConfigComboBox.DataSource = Configurations;
}
Get all fileEntries (full path), and then use Path.GetFileName() to obtain only the filename of each:
List<String> Configurations = new List<string>();
string [] fileEntries = Directory.GetFiles(path);
foreach(string fileName in fileEntries)
{
Configurations.Add(Path.GetFileName(fileName);
}
SelectConfigComboBox.DataSource = Configurations;
Before set DataSource use Path.GetFileName for items in result of Directory.EnumerateDirectories(path).ToList()
FileInfo has a Name property that only contains the filename part.
var files= new DirectoryInfo(path).GetFiles("*");
var firstFilename = files[0].Name;
I have a toolstrip that will contain toolstripdropdownbuttons that represent folders that are found in a directory. I would like a dropdown menu to each toolstripdropdownbutton to contain subfolders that are found. An example of this would be the Internet Explorer Links bar I have tried the following code but I'm not quite sure how to go about it (see picture)
Links Bar Example
Code I have tried:
private void populateLinks()
{
linksToolStrip.Items.Clear();
DirectoryInfo linksFolder = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\\Links");
foreach (DirectoryInfo linksDirectory in linksFolder.GetDirectories())
{
Image favImage = Properties.Resources.Folder;
ToolStripDropDownButton button = new ToolStripDropDownButton();
button.Text = Truncate(linksDirectory.Name, 22);
button.ToolTipText = linksDirectory.Name + "\nDate Created: " + Directory.GetCreationTime(linksDirectory.FullName);
button.Image = favImage;
button.Tag = linksDirectory.FullName;
linksToolStrip.Items.Add(button);
populateLinksFolders(linksDirectory, button.DropDown);
}
and
private void populateLinksFolders(DirectoryInfo subdirectory, ToolStripDropDown tsdd)
{
foreach (DirectoryInfo directory in subdirectory.GetDirectories())
{
populateLinksFolders(directory, ?) //<- Everything tried here fails
}
}
How can I accomplish this?
You need to pass the ToolStripDropDownButton, then use the DropDownItems property to add subitems.
populateLinksFolders(linksDirectory, button);
Then:
private void populateLinksFolders(DirectoryInfo subdirectory, ToolStripDropDownButton tsddb)
{
foreach (DirectoryInfo directory in subdirectory.GetDirectories())
{
ToolStripDropDownButton button = new ToolStripDropDownButton(directory.Name);
tsddb.DropDownItems.Add(button);
populateLinksFolders(directory, button);
}
}
I hope you can help me with this problem.
I've been trying to fill a combobox with the namefiles of a specific directory. This DIR will be always the same so it will be the same routine always.
Any ideas?
Cheers!
string[] filePaths = Directory.GetFiles(#"c:\MyDir\", "*.txt");
foreach (string file in filePaths)
{
mycombobox.items.add(file);
}
When you initialize do this:
private void Form1_Load(object sender, EventArgs e)
{
string[] files = System.IO.Directory.GetFiles(#"C:\Testing");
this.comboBox1.Items.AddRange(files);
}
Or if you are using WPF
<Grid>
<ComboBox x:Name="DirectoriesComboBox" Width="100" Height="25"></ComboBox>
</Grid>
string [] array = Directory.GetFiles(#"C:\Test");
DirectoriesComboBox.ItemsSource = array;
You can do so by adding a reference to system.IO and using this code:
(DDLFolder is you dropdown list, and if you are writing an ASP.Net application for getting the path use Server.Mappath("~/yourpath"))
DirectoryInfo df = new DirectoryInfo(userFolderPath);
DDLFolder.Items.Clear();
DDLFolder.Items.Add("Root");
foreach (DirectoryInfo d in df.GetDirectories())
{
DDLFolder.Items.Add(d.Name);
}