开发者

C# Insert directory structure into a rich text box on form

开发者 https://www.devze.com 2023-03-10 10:54 出处:网络
private void button6_Click(object sender, EventArgs e) { string[] filePaths = Directory.GetFiles(@\"c:\\MyDir\\\");
private void button6_Click(object sender, EventArgs e)
{
    string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
}

And I have a RichTextBox:

private void richTextBox4_TextChanged(object sender, EventArgs e)
{
}

How can I return / output the data 开发者_JAVA百科from the method into the RichTextBox?


Once you get the filenames, you can use Enumerable.Aggregate to turn them into a string and set that string as the text for the Rich Text Box:

private void button6_Click(object sender, EventArgs e)
{
    richTextBox4.Text = 
        Directory.GetFiles(@"C:\MyDir\")
                 .Aggregate("", (text, pathName) => 
                     text += String.Format("{0}\n", pathName))
                  );
}


foreach(string path in filePaths) {
    richTextBox4.AppendText(path + Environment.NewLine);
}


string[] filePaths = Directory.GetFiles(@"c:\MyDir\");
richTextBox4.Text = string.Join(Environment.NewLine, filePaths);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号