Reputation: 351
I have a library class project with a User Control on the user control i have explorer like windows explorer i can select a directory and right click i have ContextMenuStrip menu with Edit Paste Copy Upload...
When i click on Upload i need to get from the item(directory) i clicked on all the directory sub directories and all files. I tried to do it like this:
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
AllFiles = System.IO.Directory.GetFiles(e.ClickedItem.Text, "*.*", System.IO.SearchOption.AllDirectories);
Bgw.RunWorkerAsync();
}
}
But e.ClickedItem.Text is not the directory i selected but the "Upload" menu text.
What i need is that AllFiles(String[]) will contain all sub directories and all files so i can use it later on form1. In general in form1 i want to upload the whole complete selected directory and all sub directories and files to my ftp.
I just need to find how to get it all.
I'm using for the right click to select the directory treeView. This is the Mouse Up event:
void treeView1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
if (treeView1.SelectedNode != null)
{
menuStrip.Show(treeView1, e.Location);
}
}
}
And the After Select event:
void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
AddDirectories(e.Node);
AddFiles(e.Node.FullPath.ToString());
statusBar1.Text = iDirectories.ToString() + " Folder(s) " + iFiles.ToString() + " File(s)";
}
Upvotes: 1
Views: 10018
Reputation: 295
private void dgv1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenuStrip m = new ContextMenuStrip();
m.Items.Add("Add");
m.Items.Add("Delete");
m.Show(dgv1, new Point(e.X, e.Y));
m.ItemClicked += new ToolStripItemClickedEventHandler(Item_Click);
}
}
private void Item_Click(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Delete")
{
//Codes Here
}
else
{
//Codes Here
}
}
Upvotes: 0
Reputation: 1518
As you do not provide too much information, I'll be "generic". Consider your event handler:
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
//do something
}
}
Your question is how can you recover the informartion you need from your user control to effectivelly "do something". You can do the following:
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
var contextMenu = sender as ContextMenuStrip;
var yourControl = contextMenu.SourceControl as TypeOfYourControl;
//Get information from your control and "do something"
}
}
Assuming your control has a public string Property called SelectedDirectory that contains the user selection, you can do something like this
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
var contextMenu = sender as ContextMenuStrip;
var yourControl = contextMenu.SourceControl as TypeOfYourControl;
AllFiles = System.IO.Directory.GetFiles(yourControl.SelectedDirectory, "*.*", System.IO.SearchOption.AllDirectories);
Bgw.RunWorkerAsync();
}
}
But here I must point you that "GetFiles will return ONLY files. It will not return subdirectories. You can recover the directories from the path, but if there are empty directories, you will not have those ones in the list.
So, in any case, the best is to just pass to the "worker" the path and let the worker iterate through the sub-directories using a mix of GetFiles and GetDirectories.
OR...
If you are using DotNet >= 4.0, you can use GetFileSystemInfos instead:
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "Upload")
{
var contextMenu = sender as ContextMenuStrip;
var yourControl = contextMenu.SourceControl as TypeOfYourControl;
AllFiles = System.IO.Directory.GetFileSystemInfos(yourControl.SelectedDirectory, System.IO.SearchOption.AllDirectories);
Bgw.RunWorkerAsync();
}
}
In this case you have to change your AllFiles type to FileSystemInfo[] type.
Upvotes: 3