ctrans.cs

来自「用于文件传输的Web服务客户端」· CS 代码 · 共 119 行

CS
119
字号
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Configuration;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
namespace E3.Client.SZSEClient
{
	/// <summary>
	/// Summary description for CTrans.
	/// </summary>	
	public class CTrans
	{		
		[DllImport("kernel32.dll", SetLastError=true)] static extern void ExitProcess(int uExitCode);
		public CTrans()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		public static void startTransAllFiles()
		{
			string sUrl=ConfigurationSettings.AppSettings["ServiceAddress"];
			string UserName=ConfigurationSettings.AppSettings["UserName"];
			string PassWord=ConfigurationSettings.AppSettings["PassWord"];
			string TransPath=ConfigurationSettings.AppSettings["TransPath"];
			string MovePath=ConfigurationSettings.AppSettings["MovePath"];
			E3.WebService.SZSE.InterFace  svr=new E3.WebService.SZSE.InterFace(sUrl);
			if((UserName!=null&&PassWord!=null)&&(UserName!=""&&PassWord!=""))
			{
				svr.Credentials=new System.Net.NetworkCredential(UserName,PassWord);						
			}
			if(TransPath!=null&&TransPath!="")
			{
				string[] txtPath=Directory.GetFiles(TransPath);				
				foreach(string f in txtPath)
				{
					try
					{						
						FileInfo fin=new FileInfo(f);
						string fileName=fin.Name;						
						FileStream fs =new FileStream(f,FileMode.Open,FileAccess.Read);
						fs.Seek(0,SeekOrigin.Begin);
						byte[] b =new byte[fs.Length];
						fs.Read(b,0,b.Length);
						fs.Close();					
						svr.Timeout=3600000;
						string sr=svr.TransFile(fileName,b);										
						if(sr==""&&MovePath!="")
						{
							try
							{
								if(File.Exists(MovePath+fileName))
								{
									File.Delete(MovePath+fileName);
								}
								fin.MoveTo(MovePath+fileName);
							}
							catch
							{								
							}							
						}
						else
						{
							//MessageBox.Show(sr,"E3 Error Messages");
							fin.Delete();									
						}						
					}
					catch(System.Exception ex)
					{		
						ExitProcess(0);
						//MessageBox.Show(ex.Message,"E3 Error Messages");
						return;						
					}						
				}
				ExitProcess(1);
				//MessageBox.Show("Trans File Successfully!","E3 Error Messages");
			}	
		}
		public static void TransFile(string f)
		{
			FileInfo fin=new FileInfo(f);
			string fileName=fin.Name;
			FileStream fs =new FileStream(f,FileMode.Open,FileAccess.Read);
			fs.Seek(0,SeekOrigin.Begin);
			byte[] b =new byte[fs.Length];
			fs.Read(b,0,b.Length);
			fs.Close();		

			string sUrl=ConfigurationSettings.AppSettings["ServiceAddress"];
			string UserName=ConfigurationSettings.AppSettings["UserName"];
			string PassWord=ConfigurationSettings.AppSettings["PassWord"];
			string TransPath=ConfigurationSettings.AppSettings["TransPath"];
			string MovePath=ConfigurationSettings.AppSettings["MovePath"];
			E3.WebService.SZSE.InterFace svr=new E3.WebService.SZSE.InterFace(sUrl);
			if((UserName!=null&&PassWord!=null)&&(UserName!=""&&PassWord!=""))
			{
				svr.Credentials=new System.Net.NetworkCredential(UserName,PassWord);						
			}
			string sr=svr.TransFile(fileName,b);
			if(sr==""&&MovePath!="")
			{
				if(File.Exists(MovePath+fileName))
				{
					File.Delete(MovePath+fileName);
				}
				fin.CopyTo(MovePath+fileName);				
			}
			else
			{
				MessageBox.Show(sr,"E3 Error Messages");
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?