質問

I am relatively new in C#. My question is: I have created a MenuStrip. I want to create with ButtonCreate_Click a Folder under the Directory path. So how can use the path in the Function buttonCreate? Is that possible?

    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);
    }
役に立ちましたか?

解決

Because you are declaring patheto in the menu items click event it is only a local variable to that scope. If you create a property for your form, then that property can be used within the forms scope. Something like this:

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);
}

This means that your variable patheto can be accessed anywhere within the form. What you have to remember is that wherever you declare your variables, they will only be accessible in that function/class or child functions/methods.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top