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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何比较变量并将变量设置为在c#中的一个变量中找到的新文本相关的知识,希望对你有一定的参考价值。

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

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

以上是关于如何比较变量并将变量设置为在c#中的一个变量中找到的新文本的主要内容,如果未能解决你的问题,请参考以下文章

如果全局不起作用,在Python中访问for循环之外的变量?

如何从 1 数到 50 并将每个数字设置为批处理文件中的变量?

PHP - 将会话变量设置为在 30 分钟后过期 [重复]

如何将结构字段与C中的变量进行比较? [复制]

字符型变量C为啥直接可以和ASCII码比较呢?

如何将函数中的短局部变量设置为公共