文件提取器带来所有文件,而不仅仅是 zip 文件
File extractor brings all files instead of just zip file
我创建了一个文件提取器,现在它可以工作了,但是它还会将所有文件从 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();
}
}
}
程序后的目录图像是运行:
当您调用
时,您的代码正在目标目录中创建一个 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";
我创建了一个文件提取器,现在它可以工作了,但是它还会将所有文件从 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();
}
}
}
程序后的目录图像是运行:
当您调用
时,您的代码正在目标目录中创建一个 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";