如何比较变量并将变量设置为在c#中的一个变量中找到的新文本

问题描述 投票:-2回答:1

我需要将目录中找到的文件与另一个目录进行比较,并将目录中找到的文件新文件打印到控制台中。我有它设置所以它需要文件并将其分配给变量。另一个目录被分配给另一个变量。如何在其中一个变量中打印新数据到命令提示符?我使用的语言是c#。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Write the path of the old file folder: ");
            string path = Console.ReadLine();
            listFilesFromDirectory(path);


            Console.Write("Write the path of the new file folder: ");
            string pathTwo = Console.ReadLine();
            listFilesFromDirectoryMore(pathTwo);
            Console.WriteLine("Press Enter to continue:");
            Console.Read();
        }

        static void listFilesFromDirectory(string workingDirectory)
        {
            // get a list of files in a directory, 
            // based upon a path that is passed into the function
            string[] filePaths = Directory.GetFiles(workingDirectory);
            foreach (string filePath in filePaths)
            {
                // use the foreach loop to go through the entire 
                // array one element at a time write out 
                // the full path and file name of each of the 
                // elements in the array
                Console.WriteLine(filePath);
                string oldfile = (filePath);

            }
        }

        static void listFilesFromDirectoryMore(string workingDirectoryTwo)
        {
            // get a list of files in a directory, 
            // based upon a path that is passed into the function
            string[] filePathsTwo = Directory.GetFiles(workingDirectoryTwo);
            foreach (string filePathTwo in filePathsTwo)
            {
                // use the foreach loop to go through the entire 
                // array one element at a time write out 
                // the full path and file name of each of the 
                // elements in the array
                Console.WriteLine(filePathTwo);
                string newfile = (filePathTwo);

            }
        }
    }
}
c# string variables compare
1个回答
1
投票
using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Write the path of the old file folder: ");
            string pathA = Console.ReadLine();

            Console.Write("Write the path of the new file folder: ");
            string pathB = Console.ReadLine();
            Console.WriteLine("Press Enter to continue:");

            List<string> folderA = new List<string>();
            foreach (string filePath in Directory.GetFiles(pathA))
            {
                folderA.Add(Path.GetFileName(filePath));
            }

            List<string> folderB = new List<string>();
            foreach (string filePath in Directory.GetFiles(pathB))
            {
                folderB.Add(Path.GetFileName(filePath));
            }

            Console.Write("New files in folder" + pathA + " : ");
            Print(folderA, folderB);

            Console.WriteLine("------------------------------------");

            Console.Write("New files in folder" + pathB + " : ");
            Print(folderB, folderA);

            Console.Read();
        }

        static void Print(List<string> lstA, List<string> lstB)
        {
            foreach (string fileName in lstA)
            {
                if (!lstB.Contains(fileName))
                {
                    Console.Out.WriteLine(fileName);
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.