C# 需要从预定义位置压缩文本文件&;使用windows服务将压缩文件保存到硬盘的另一个位置
需要从预定义位置压缩文本文件,并使用windows服务将压缩后的文件保存在硬盘的另一个位置,同时删除旧文本文件(计划服务) 我尝试了下面的代码,其中我使用了“icsharpcode”来压缩文件。但是在安装了服务之后。。我收到一条消息,“此服务已启动,然后停止…”,但没有显示任何必需的输出C# 需要从预定义位置压缩文本文件&;使用windows服务将压缩文件保存到硬盘的另一个位置,c#,asp.net,scheduled-tasks,C#,Asp.net,Scheduled Tasks,需要从预定义位置压缩文本文件,并使用windows服务将压缩后的文件保存在硬盘的另一个位置,同时删除旧文本文件(计划服务) 我尝试了下面的代码,其中我使用了“icsharpcode”来压缩文件。但是在安装了服务之后。。我收到一条消息,“此服务已启动,然后停止…”,但没有显示任何必需的输出 using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Diagnostics.Design
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Design;
using System.Diagnostics.Eventing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using ICSharpCode.SharpZipLib.Zip;
namespace createzip
{
public partial class createzip : ServiceBase
{
Timer timer1 = new Timer();
public createzip()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1.Elapsed += new ElapsedEventHandler(onelapsedtime);
timer1.Enabled = true;
timer1.Interval = 60000;
}
protected override void OnStop()
{
您必须在
OnStart()函数中启动计时器:timer1.start()
一个问题是,如果这是一个Windows服务,您可能需要在start方法中有一个无限循环,以便该服务永久运行?一个更简单、更易于维护的解决方案是将其转换为控制台应用程序,取出时间代码并使用Task Scheduler调用它。@RononDex我尝试过,它不起作用g还有那一行代码奇怪,你把那一行放在OnStart()的末尾了吗?应该是这样的。我自己在windows服务中用过几次这段代码。@RononDex不起作用。。我卡住了。
timer1.Enabled = false;
}
private void onelapsedtime(object source, ElapsedEventArgs e)
{
string folder = "@E:\\zipped files";
Directory.SetCurrentDirectory(folder);
string output = "@E:\\output";
string outputfilename = Path.Combine(output, "this file is zipped");
using (var x = new ZipFile(output))
{
foreach (var f in Directory.GetFiles(folder))
x.Add(f);
}
string[] filenames = Directory.GetFiles(folder);
using ( ZipOutputStream s = new
ZipOutputStream(File.Create(output))) //(args[1])))
{
s.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetDirectoryName(file));
//entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourcebytes;
do
{
sourcebytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourcebytes);
}
while (sourcebytes > 0);
}
}
s.Finish();
s.Close();
return;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Timers;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
namespace trail2zip
{
public partial class trail2zip : ServiceBase
{
Timer timer;
string path1 = @"E:\zipped files\New Text Document.txt";
string path2 = @"E:\output\filezipname.zip";
string path3 = @"E:\zipped files\R_23122015.txt";
int timerInterval = 60000;
public trail2zip()
{
InitializeComponent();
timer = new Timer();
timer.Elapsed+=new ElapsedEventHandler(this.timer_Elapsed);
timer.Interval = timerInterval;
timer.Enabled = true;
}
protected override void OnStart(string[] args)
{
timer.Start();
}
protected override void OnStop()
{
timer.Stop();
timer.SynchronizingObject = null;
timer.Elapsed -= new ElapsedEventHandler(this.timer_Elapsed);
timer.Dispose();
timer = null;
}
public void timer_Elapsed(object sender, ElapsedEventArgs e)
{
ZipFile z = ZipFile.Create(path2); //(filezipname);
z.BeginUpdate();
z.Add(path1);
z.Add(path3);
z.CommitUpdate();
z.Close();
}
}
}