当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Adding Menu Subitems By Getting File Names from Directory

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: