c#建立windows服務並定時傳送檔案到java伺服器

NO IMAGE

建立c#windows服務是參照https://blog.csdn.net/xgf415/article/details/53786431文件做的,啟發很大

建立定時是參照https://www.cnblogs.com/goto/p/4172274.html文件做的

最終合併程式碼如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.ServiceProcess;
using System.Text;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer1;  //計時器
//自定義一個類
public class FileTimeInfo
{
public string FileName;  //檔名
public DateTime FileCreateTime; //建立時間
}
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new System.Timers.Timer();
timer1.Interval = 3000;  //設定計時器事件間隔執行時間
timer1.Elapsed  = new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Enabled = true;
}
protected override void OnStop()
{
this.timer1.Enabled = false;
}
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//使用 GetLatestFileTimeInfo
//獲取d:\test檔案中,副檔名為.pdf的最新檔案
FileTimeInfo fi = GetLatestFileTimeInfo(@"d:\test", ".pdf");
if (fi != null)
{
Console.WriteLine("檔名:{0} 建立時間:{1}", fi.FileName, fi.FileCreateTime);
//Console.WriteLine(Path.GetFileName(fi.FileName));
}
else
{
Console.WriteLine("資料夾中沒有指定副檔名的檔案!");
}
StartSend(fi.FileName, Path.GetFileName(fi.FileName));
//String file = @"d://test//test1.pdf";
//建立PdfDocument例項
/**
PdfDocument pdf = new PdfDocument();
//載入PDF文件  
pdf.LoadFromFile(fi.FileName);
//獲取第一頁  
PdfPageBase page = pdf.Pages[0];
//從第一頁的指定矩形區域內提取文字  
string text = page.ExtractText(new RectangleF(50, 50, 500, 100));
//儲存文字到.txt檔案  
StringBuilder sb = new StringBuilder();
sb.AppendLine(text);
File.WriteAllText(@"d://Extract233.txt", sb.ToString());
**/
}
//獲取最近建立的檔名和建立時間
//如果沒有指定型別的檔案,返回null
static FileTimeInfo GetLatestFileTimeInfo(string dir, string ext)
{
List<FileTimeInfo> list = new List<FileTimeInfo>();
DirectoryInfo d = new DirectoryInfo(dir);
foreach (FileInfo fi in d.GetFiles())
{
if (fi.Extension.ToUpper() == ext.ToUpper())
{
list.Add(new FileTimeInfo()
{
FileName = fi.FullName,
FileCreateTime = fi.CreationTime
});
}
}
var qry = from x in list
orderby x.FileCreateTime
select x;
return qry.LastOrDefault();
}
static int StartSend(string path, string fileName)
{
if (!File.Exists(path))
{
return -1;
}
NetworkStream stream = null;
BinaryWriter sw = null;
FileStream fsMyfile = null;
BinaryReader brMyfile = null;
try
{
TcpClient client = new TcpClient("192.168.1.112", 8888);
stream = client.GetStream();
sw = new BinaryWriter(stream);
///取得檔名位元組陣列
byte[] fileNameBytes = Encoding.Default.GetBytes(fileName);
byte[] fileNameBytesArray = new byte[1024];
Array.Copy(fileNameBytes, fileNameBytesArray, fileNameBytes.Length);
///寫入流
sw.Write(fileNameBytesArray, 0, fileNameBytesArray.Length);
sw.Flush();
///獲取檔案內容位元組陣列
///byte[] fileBytes = returnbyte(filePath);
fsMyfile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
brMyfile = new BinaryReader(fsMyfile);
///寫入流
byte[] buffer = new byte[1024];
int count = 0;
while ((count = brMyfile.Read(buffer, 0, 1024)) > 0)
{
sw.Write(buffer, 0, count);
sw.Flush();
buffer = new byte[1024];
}
}
catch (SocketException se)
{
Console.WriteLine(se.StackTrace);
return -2;
}
catch (IOException ioe)
{
Console.WriteLine(ioe.StackTrace);
return -3;
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
return -4;
}
finally
{
if (sw != null)
{
sw.Close();
}
if (brMyfile != null)
{
brMyfile.Close();
}
if (fsMyfile != null)
{
fsMyfile.Close();
}
if (stream != null)
{
stream.Close();
}
}
return 0;
}
}
}