公用类库代码 常用方法<br> namespace
UtilityLibary
{
public
interface ILog
{
int
FileSize { get ; set ; }
/// <summary>
/// 写入log到指定文件
/// </summary>
/// <param name="msg"></param>
/// <param name="fullpath"></param>
void
Log( string
msg, string
fullpath);
}
public
interface INetHelper
{
HttpWebRequest WebRequestInstance { get ; set ; }
HttpWebResponse WebResponseInstance { get ; set ; }
string
Url { get ; set ; }
}
public
interface IUtility
{
/// <summary>
/// md5 加密 返回信息都是小写
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
string
MD5Encrypt( string
input);
/// <summary>
/// 转化时间为时间戳
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
long
ConvertDateTimeToLong(DateTime dt);
/// <summary>
/// 转化时间戳为时间
/// </summary>
/// <param name="times"></param>
/// <returns></returns>
DateTime ConvertLongToDateTime( long
times);
}
public
interface IFile
{
string
GetCurrentDirectory();
string
GetSystemDriveDirectory();
string
GetCurrentWorkDirectory();
}
public
abstract class FileBase : IFile
{
public
string GetCurrentDirectory()
{
return
System.Environment.CurrentDirectory;
}
public
string GetSystemDriveDirectory()
{
throw
new NotImplementedException();
}
public
string GetCurrentWorkDirectory()
{
return
System.IO.Directory.GetCurrentDirectory();
}
}
public
class FileHelper : FileBase
{
}
public
class LogHelper : ILog
{
private
object mux = new
object ();
public
int FileSize
{
get ;
set ;
}
public
void Log( string
msg, string
fullpath)
{
lock
(mux)
{
if
(!File.Exists(fullpath))
{
using
(File.Create(fullpath)) { }
}
using
(StreamWriter sw = new
StreamWriter(fullpath))
{
sw.WriteLine(msg);
}
}
}
public
void Log( string
msg, string
directoryPath, bool
autoCreateDateDirectory = true )
{
lock
(mux)
{
if
(autoCreateDateDirectory)
{
string
dateDirectory = Path.Combine(directoryPath, DateTime.Now.ToString( "yyyyMMdd" ));
if
(!Directory.Exists(dateDirectory))
{
Directory.CreateDirectory(directoryPath);
}
string
txtPath = Path.Combine(dateDirectory, "log.txt" );
if
(!File.Exists(txtPath))
{
using
(File.Create(txtPath)) { }
}
using
(StreamWriter sw = new
StreamWriter(txtPath, true , Encoding.UTF8))
{
sw.WriteLine(msg);
}
}
}
}
}
public
class NetHelper : INetHelper
{
public
HttpWebRequest WebRequestInstance
{
get ;
set ;
}
public
HttpWebResponse WebResponseInstance
{
get ;
set ;
}
public
string Url
{
get ;
set ;
}
public
delegate void RequestStreamHandle(HttpWebRequest request);
/// <summary>
/// 对请求流做操作
/// </summary>
public
event RequestStreamHandle RequestStreamEvent;
public
delegate void ResponseStreamHandle(HttpWebResponse request);
/// <summary>
/// 对返回流做处理
/// </summary>
public
event ResponseStreamHandle ResponseStreamEvent;
public
delegate void UserAgengtHandle(HttpWebRequest request);
public
event UserAgengtHandle UserAgengtEvent;
public
NetHelper( string
url)
{
this .Url = url;
}
public
void GetResponse()
{
if
(! string .IsNullOrEmpty(Url))
{
throw
new Exception( "Url can‘t be empty" );
}
WebRequestInstance = WebRequest.Create( this .Url) as
HttpWebRequest;
WebRequestInstance.Method = "POST" ;
WebRequestInstance.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36" ;
if
(UserAgengtEvent != null )
{
UserAgengtEvent(WebRequestInstance);
}
if
(RequestStreamEvent != null )
{
RequestStreamEvent(WebRequestInstance);
}
try
{
WebResponseInstance = WebRequestInstance.GetResponse() as
HttpWebResponse;
}
catch
(Exception ex)
{
if
(ex is
WebException)
{
WebResponseInstance = (ex as
WebException).Response as
HttpWebResponse;
}
}
if
(ResponseStreamEvent != null )
{
ResponseStreamEvent(WebResponseInstance);
}
}
}
public
class Utility : IUtility
{
public
string MD5Encrypt( string
input)
{
byte [] result = Encoding.Default.GetBytes(input); //tbPass为输入密码的文本框
MD5 md5 = new
MD5CryptoServiceProvider();
byte [] output = md5.ComputeHash(result);
return
BitConverter.ToString(output).Replace( "-" , "" ).ToLower();
}
public
long ConvertDateTimeToLong(DateTime dt)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime( new
System.DateTime(1970, 1, 1));
return
( long )(dt - startTime).TotalSeconds;
}
public
DateTime ConvertLongToDateTime( long
times)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime( new
DateTime(1970, 1, 1));
long
lTime = long .Parse(times + "0000000" );
TimeSpan toNow = new
TimeSpan(lTime); return
dtStart.Add(toNow);
}
}
}
|