如何在许多字符串之间划一条线 [英] How do I make a line between lots of strings

查看:96
本文介绍了如何在许多字符串之间划一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个程序,可以在两个文件夹中找到新文件,但这就是它输出的内容:file.txtfile.txtfile.txt



我希望它输出:

file.txt

file.txt

file.txt



请有人帮帮我



我尝试了什么:



i have made a program that finds new files in two folders however, this is what it outputs: file.txtfile.txtfile.txt

and i would like it to output:
file.txt
file.txt
file.txt

please can someone help me

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fdb = new FolderBrowserDialog();
            if (fdb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                oldtextbox.AppendText(fdb.SelectedPath);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string oldpath = oldtextbox.Text;
            string newpath = newtextbox.Text;

            string str = oldpath;
            string str1 = newpath;
            List<string> strs = new List<string>();
            string[] files = Directory.GetFiles(str);
            for (int i = 0; i < (int)files.Length; i++)
            {
                strs.Add(Path.GetFileName(files[i]));
            }
            List <string> strs1 = new List<string>();
            string[] strArrays = Directory.GetFiles(str1);
            for (int j = 0; j < (int)strArrays.Length; j++)
            {
                strs.Add(Path.GetFileName(strArrays[j]));
            }
#T H E   T E X T   I S   U N D E R   H E R E
            richTextBox1.AppendText(string.Concat(strs));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fdb = new FolderBrowserDialog();
            if (fdb.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                newtextbox.AppendText(fdb.SelectedPath);
        }
    }

}

推荐答案

假设您的文本框设置为多行...



Assuming your text box is set to multi-line...

newtextbox.AppendText("\n");





编辑:根据附加信息,这是修改后的解决方案。



看来你正试图连接文件来自两个不同目录的名称(无用地命名为 str str1 )。



首先,我将使用LINQ构建文件名序列(确保包含一个用于System.Linq的文件)。





Based on additional information, this is the revised solution.

It appears you are trying to concatenate the file names from two different directories (unhelpfully named str and str1).

First, I would build the sequence of file names using LINQ (make sure to include a using for System.Linq).

List<string> fileNames = Directory.EnumerateFiles(str)
  .Concat(Directory.EnumerateFiles(str1))
  .Select(path => Path.GetFileName(path))
  .ToList();

richTextBox1.AppendText(string.Join("\n", fileNames));





I我会花一点时间来解释这些部分。 Directory.EnumerateFiles(str)返回指定路径( str )中文件路径的枚举。



然后, .Concat(Directory.EnumerateFiles(str1))连接第二个指定路径中的文件路径枚举( str1 )到原始枚举的末尾。



下一步,。选择(路径=> Path.GetFileName(path))修改连接的枚举,使其现在包含每个路径的文件名。



接下来, .ToList()从枚举中创建一个列表。



最后, string.Join(\ n,fileNames)在fileNames的每个元素之间放置一个换行符号。



我强烈建议你服用学习LINQ的时刻。当你操作项目序列时它非常强大。



I'll take a sec to explain the parts. Directory.EnumerateFiles(str) returns an enumeration of file paths in the specified path (str).

Then, .Concat(Directory.EnumerateFiles(str1)) concatenates an enumeration of file paths in a second specified path (str1) onto the end of the original enumeration.

Next, .Select(path => Path.GetFileName(path)) modifies the concatenated enumeration so that it now consists of the file name for each path.

Next, .ToList() creates a list from the enumeration.

Finally, string.Join("\n", fileNames) places a new line character between each element of fileNames.

I highly suggest taking a moment to learn LINQ. Its very powerful when you are manipulating sequences of items.


这篇关于如何在许多字符串之间划一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆