我已经创建了一个文件提取器,现在它确实有效,但是它也会将所有文件从startDir
移动到destDir
以及zip文件。如何让这个程序只移动zip文件而不是所有文件?
来源:
using System;
using System.IO.Compression;
namespace ArchiveCreator
{
class Program
{
//When program is run successfully this will be the output
public string Success(string input)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(input);
return input;
}
//When program encounters an error this will be the output
public string Warn(string input)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(input);
return input;
}
//When program has information to show, this will be the output
public string Say(string input)
{
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine(input);
return input;
}
//Main method
static void Main(string[] args)
{
//These variables are used to create a
//random string that will be used as the
//zip files name
var chars = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var stringChars = new char[8];
var random = new Random();
//Info is used as provide the type of
//information that will be displayed
//by the program
Program info = new Program();
//Create the zip file name
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
string finalString = new String(stringChars);
info.Say("Starting file extraction..");
string startDir = @"c:/users/thomas_j_perkins/test_folder";
string destDir = @"c:/users/thomas_j_perkins/archive/";
string zipName = $"c:/users/thomas_j_perkins/archive/{finalString}.zip";
try
{
ZipFile.CreateFromDirectory(startDir, zipName);
ZipFile.ExtractToDirectory(zipName, destDir);
}
catch (Exception e)
{
info.Warn($"Error: {e}");
}
info.Success($"Extracted files successfully to: {destDir}");
info.Say("Press enter to exit..");
Console.ReadLine();
}
}
}
程序运行后的目录图像:
答案 0 :(得分:1)
当您致电
时,您的代码正在目标目录中创建一个zip文件ZipFile.CreateFromDirectory(startDir, zipName);
zipname路径位于destDir中。你的意思是把它放在startDir中吗?
string startDir = @“c:/ users / thomas_j_perkins / test_folder”;
string destDir = @“ c:/ users / thomas_j_perkins / archive / ”;
string zipName = $“ c:/ users / thomas_j_perkins / archive / {finalString} .zip”;