我是C#的新手。我的问题是:我已经创建了一个MenuStrip。我想用ButtonCreate_Click在目录路径下创建一个文件夹。那么如何在函数buttonCreate中使用路径呢?这有可能吗?
    private void buttonCreate_Click(object sender, EventArgs e)
    {
        string MyFileName = "Car.txt";
        string newPath1 = Path.Combine(patheto, MyFileName);
        //Create a folder in the active Directory
        Directory.CreateDirectory(newPath1);
    }
    private void DirectoryPathToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string patheto = @"C:\temp";
        Process.Start(patheto);
    }发布于 2012-06-20 18:46:14
因为您在菜单项单击事件中声明了patheto,所以它只是该作用域的一个局部变量。如果为窗体创建属性,则可以在窗体范围内使用该属性。如下所示:
private string patheto = @"C:\temp";
private void buttonCreate_Click(object sender, EventArgs e)
{
    string MyFileName = "Car.txt";
    string newPath1 = Path.Combine(patheto, MyFileName);
    // Create a folder in the active Directory
    Directory.CreateDirectory(newPath1);
}
private void DirectoryPathToolStripMenuItem_Click(object sender, EventArgs e)
{
    Process.Start(patheto);
}这意味着您的变量patheto可以在表单中的任何位置访问。您必须记住的是,无论您在何处声明变量,它们都只能在该函数/类或子函数/方法中访问。
https://stackoverflow.com/questions/11117839
复制相似问题