Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
        }
    }

}
Posted
Updated 17-Jul-18 10:17am
v3

1 solution

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

newtextbox.AppendText("\n");


EDIT: 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'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.
 
Share this answer
 
v2
Comments
Member 13915301 17-Jul-18 16:07pm    
@eric lynch, where do i put that
Patrice T 17-Jul-18 16:21pm    
After the place where you add the file name
Member 13915301 17-Jul-18 16:32pm    
can you make an example with my code? please
Eric Lynch 17-Jul-18 16:56pm    
The way your code is structured it is a bit more complex. I missed that you were placing the file name in three different text boxes. I see you added a clarification. Your code is not the cleanest I've seen, but I'll try to guess at your intent. Shortly, I'll do so in an update to the solution...editing in comments is limited.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900