【问题标题】:How can I compare variables and set a variable to new text found in one of the variables in c#如何比较变量并将变量设置为在 c# 中的一个变量中找到的新文本
【发布时间】:2018-07-15 22:02:01
【问题描述】:

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

            }
        }
    }
}

【问题讨论】:

  • 所以现在你有两个文件夹中的两个文件列表,比如 lstFilesA 和 lstFilesB ?
  • 我有一个用于文件夹 A 的变量和一个用于文件夹 B 的变量
  • 这些变量里面有什么?向我们展示您的代码
  • 我能做些什么来比较变量

标签: c# string variables compare


【解决方案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);
                }
            }
        }
    }
}

【讨论】:

  • 我会试试这个程序!
  • 它显示文件夹中的所有文件,而不是新文件
  • 编辑 Console.Write("文件夹中的新文件" + filePathsTwo + " : ");Console.Write("文件夹中的新文件" + pathTwo + " : ");
  • 好的,我试试
  • 正如我所说,我没有测试就写了,所以你需要理解解决问题的想法,而不是一无所知的复制和运行它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 2016-12-03
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
相关资源
最近更新 更多