在压缩一个巨大的文件夹时,如何给其他应用程序一些磁盘时间? C#
How do I give some disk time to other applications while zipping a huge folder? C#
我有一个巨大的文件夹,每晚都必须通过应用程序压缩。但是,它会在这段时间内带走主要的磁盘性能,并且可以持续 10 分钟。我想让它成为一个线程并暂停它 2 秒,然后像循环一样再暂停 2 秒。我不介意它持续更长时间,因为我希望它仍能为其他应用程序提供一些磁盘时间
开始 > 暂停 2 秒 > 恢复 2 秒 > 暂停 2 秒 > 恢复 2 秒 >......完成
static void Main(string[] args)
{
zipFolder(@"c:\example\start", @"c:\example\result.zip");
}
static void zipFolder(string startPath, string zipPath)
{
ZipFile.CreateFromDirectory(startPath, zipPath);
}
看看https://msdn.microsoft.com/en-us/library/hh485716(v=vs.110).aspx
创建线程并在线程中添加一个条目然后暂停。
我建议使用 DotNetZip
这样的库
然后你可以像下面这样创建一个简单的包装器。
public class ScheduleZipper
{
private int _interval;
private DateTime _lastZip;
private string _source;
private string _dest;
public ScheduleZipper(string source, string dest, int interval)
{
_interval = interval;
_lastZip = DateTime.Now.AddMilliseconds(_interval);
}
private void ZipFilesInFolder(string path, ZipFile zip)
{
foreach (var file in Directory.GetFiles(path))
{
if (DateTime.Now >= _lastZip.AddMilliseconds(_interval))
{
System.Threading.Thread.Sleep(_interval);
_lastZip = DateTime.Now;
}
zip.AddFile(file);
}
foreach (var dir in Directory.GetDirectories(path))
{
ZipFilesInFolder(path, zip);
}
}
public void Zip()
{
using (var zip = new ZipFile(_dest))
{
ZipFilesInFolder(_source, zip);
}
}
}
然后简单地做这样的事情
var schedule = new ScheduleZipper(@"c:\example\start", @"c:\example\result.zip", 2000);
schedule.Zip();
如果您的程序正在做其他事情,那么您可以将其包装到一个线程中。
注意:您可能想要修改代码以创建您想要的 zip 存档,包括文件夹等。
我有一个巨大的文件夹,每晚都必须通过应用程序压缩。但是,它会在这段时间内带走主要的磁盘性能,并且可以持续 10 分钟。我想让它成为一个线程并暂停它 2 秒,然后像循环一样再暂停 2 秒。我不介意它持续更长时间,因为我希望它仍能为其他应用程序提供一些磁盘时间
开始 > 暂停 2 秒 > 恢复 2 秒 > 暂停 2 秒 > 恢复 2 秒 >......完成
static void Main(string[] args)
{
zipFolder(@"c:\example\start", @"c:\example\result.zip");
}
static void zipFolder(string startPath, string zipPath)
{
ZipFile.CreateFromDirectory(startPath, zipPath);
}
看看https://msdn.microsoft.com/en-us/library/hh485716(v=vs.110).aspx
创建线程并在线程中添加一个条目然后暂停。
我建议使用 DotNetZip
然后你可以像下面这样创建一个简单的包装器。
public class ScheduleZipper
{
private int _interval;
private DateTime _lastZip;
private string _source;
private string _dest;
public ScheduleZipper(string source, string dest, int interval)
{
_interval = interval;
_lastZip = DateTime.Now.AddMilliseconds(_interval);
}
private void ZipFilesInFolder(string path, ZipFile zip)
{
foreach (var file in Directory.GetFiles(path))
{
if (DateTime.Now >= _lastZip.AddMilliseconds(_interval))
{
System.Threading.Thread.Sleep(_interval);
_lastZip = DateTime.Now;
}
zip.AddFile(file);
}
foreach (var dir in Directory.GetDirectories(path))
{
ZipFilesInFolder(path, zip);
}
}
public void Zip()
{
using (var zip = new ZipFile(_dest))
{
ZipFilesInFolder(_source, zip);
}
}
}
然后简单地做这样的事情
var schedule = new ScheduleZipper(@"c:\example\start", @"c:\example\result.zip", 2000);
schedule.Zip();
如果您的程序正在做其他事情,那么您可以将其包装到一个线程中。
注意:您可能想要修改代码以创建您想要的 zip 存档,包括文件夹等。