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