using System;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
namespace Baolee.GeneralMethod
{
/// <summary>
/// Message 的摘要說明。
/// </summary>
public class Message
{
#region // Methods
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
public Message()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
/// <summary>
///
/// </summary>
/// <param name="msg"></param>
public static void Alert(string msg)
{
string s = "<script language='javascript' defer>alert(/"" + msg + "/");</script>";
HttpContext.Current.Response.Write(s);
}
/// <summary>
/// Alert();用于繼承自Web.UI.Page的頁面 default Key : alertmsg
/// </summary>
/// <param name="msg">message</param>
/// <param name="page">Page</param>
public static void Alert(string msg, Page page)
{
string script = "<script language='javascript' defer>alert(/"" + msg + "/");</script>";
page.RegisterStartupScript("alertmsg", script);
}
/// <summary>
/// Alert();用于繼承自Web.UI.Page的頁面, key 為標識符
/// </summary>
/// <param name="key">標識符</param>
/// <param name="msg">message</param>
/// <param name="page">Page</param>
public static void Alert(string key, string msg, Page page)
{
string script = "<script language='javascript' defer>alert(/"" + msg + "/");</script>";
page.RegisterStartupScript(key, script);
}
/// <summary>
/// 顯示消息提示對話框,并進行頁面跳轉(zhuǎn)
/// </summary>
/// <param name="page">當前頁面指針,一般為this </param>
/// <param name="msg">提示信息</param>
/// <param name="url">跳轉(zhuǎn)的目標URL</param>
public static void AlertAndRedirect(Page page, string msg, string url)
{
StringBuilder builder = new StringBuilder();
builder.Append("<script language='javascript' defer>");
builder.AppendFormat("alert('{0}');", msg);
builder.AppendFormat("top.location.href='{0}'", url);
builder.Append("</script>");
page.RegisterStartupScript("message", builder.ToString());
}
/// <summary>
/// 控件點擊 消息確認提示框
/// </summary>
/// <param name="Control">當前頁面指針,一般為this</param>
/// <param name="msg">提示信息</param>
public static void Confirm(WebControl Control, string msg)
{
Control.Attributes.Add("onclick", "return confirm(/"" + msg + "/");");
}
/// <summary>
/// 刪除指定文件 出錯時ignor
/// </summary>
/// <param name="descFilePath">目標路徑</param>
public static void DeleteFile(string descFilePath)
{
try
{
if (File.Exists(descFilePath))
{
File.Delete(descFilePath);
}
}
catch (Exception)
{
}
}
/// <summary>
/// 刪除指定目錄下所有文件
/// </summary>
/// <param name="descDirectory">目標目錄</param>
public static void DeleteFiles(string descDirectory)
{
try
{
if (Directory.Exists(descDirectory))
{
foreach (string text in Directory.GetFiles(descDirectory))
{
if (File.Exists(text))
{
File.Delete(text);
}
}
}
}
catch (Exception exception)
{
throw new Exception(exception.Message, exception);
}
}
/// <summary>
/// 記錄錯誤信息 一般被SendMailToManager調(diào)用。 文件名為 yyyyMMdd.htm格式,記錄于 ErrorLogs目錄
/// </summary>
/// <param name="logs"></param>
public static void ErrLogs(string logs)
{
logs = logs + "/r/n";
string path = PublicConst.ErrLogPath;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string text2 = DateTime.Today.ToString("yyyyMMdd");
string text3 = path + @"/" + text2 + ".htm";
try
{
if (File.Exists(text3))
{
StreamWriter writer = File.AppendText(text3);
writer.Write(logs);
writer.Close();
}
else
{
StreamWriter writer2 = File.CreateText(text3);
writer2.Write(logs);
writer2.Close();
}
}
catch
{
}
}
/// <summary>
/// 根據(jù)URL取得頁面源文件
/// </summary>
/// <param name="theUrl">theUrl</param>
/// <returns></returns>
public static string GetUrlSource(string theUrl)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(theUrl);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
return reader.ReadToEnd();
}
/// <summary>
/// 將字串以流文件輸出
/// </summary>
/// <param name="sourStr"></param>
/// <param name="fileName"></param>
public void OutPutDownload(string sourStr, string fileName)
{
byte[] bytes = Encoding.Default.GetBytes(sourStr);
HttpContext.Current.Response.AddHeader("Content-Type", "text/html");
string text = HttpUtility.UrlEncode(Encoding.Default.GetBytes(fileName));
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Concat(new object[] { "inline;filename=", Convert.ToChar(0x22), text, Convert.ToChar(0x22) }));
int length = bytes.Length;
HttpContext.Current.Response.AddHeader("Content-Length", length.ToString());
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
/// <summary>
/// 將字串以流文件輸出,在IE中打開
/// </summary>
/// <param name="sourStr"></param>
/// <param name="fileName"></param>
/// <param name="fileType">要輸出的文件類型 eg:application/octet-stream</param>
public void OutPutDownload(string sourStr, string fileName, string fileType)
{
byte[] bytes = Encoding.Default.GetBytes(sourStr);
HttpContext.Current.Response.AppendHeader("Content-Type", fileType);
string text = HttpUtility.UrlEncode(Encoding.Default.GetBytes(fileName));
HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Concat(new object[] { "inline;filename=", Convert.ToChar(0x22), text, Convert.ToChar(0x22) }));
int length = bytes.Length;
HttpContext.Current.Response.AppendHeader("Content-Length", length.ToString());
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
/// <summary>
/// 彈出下載框
/// </summary>
/// <param name="b"></param>
/// <param name="fileName"></param>
/// <param name="fileType">eg: application/ms-excel</param>
public void OutPutDownloadPop(byte[] b, string fileName, string fileType)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = fileType;
string text = HttpUtility.UrlEncode(Encoding.Default.GetBytes(fileName));
HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Concat(new object[] { "attachment;filename=", Convert.ToChar(0x22), text, Convert.ToChar(0x22) }));
int length = b.Length;
HttpContext.Current.Response.AppendHeader("Content-Length", length.ToString());
HttpContext.Current.Response.BinaryWrite(b);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
/// <summary>
/// 彈出下載框
/// </summary>
/// <param name="sourStr"></param>
/// <param name="fileName"></param>
/// <param name="fileType">eg: application/ms-excel</param>
public void OutPutDownloadPop(string sourStr, string fileName, string fileType)
{
byte[] b = Encoding.Default.GetBytes(sourStr);
this.OutPutDownloadPop(b, fileName, fileType);
}
/// <summary>
/// 彈出下載框,按編碼,配合 CharSet.ToCHT 等使用
/// </summary>
/// <param name="sourStr"></param>
/// <param name="fileName"></param>
/// <param name="fileType">eg: text/plain</param>
/// <param name="charSet">GB2312/BIG5</param>
public void OutPutDownloadPop(string sourStr, string fileName, string fileType, string charSet)
{
byte[] b = Encoding.GetEncoding(charSet).GetBytes(sourStr);
this.OutPutDownloadPop(b, fileName, fileType);
}
/// <summary>
/// 讀取指定路徑的文件,以GB2312格式
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static string ReadText(string filePath)
{
string text = "";
FileStream stream = File.OpenRead(filePath);
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GB2312"));
while (reader.Peek() != -1)
{
text = reader.ReadToEnd();
}
reader.Close();
stream.Close();
return text;
}
/// <summary>
/// 讀取指定路徑的文件,按參數(shù)編碼
/// </summary>
/// <param name="filePath"></param>
/// <param name="charSet">GB2312/Big5</param>
/// <returns></returns>
public static string ReadTextANSI(string filePath, string charSet)
{
string text = "";
FileStream stream = File.OpenRead(filePath);
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("charSet"));
while (reader.Peek() != -1)
{
text = reader.ReadToEnd();
}
reader.Close();
stream.Close();
return text;
}
/// <summary>
/// 讀取指定路徑的文件,以UTF格式
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static string ReadTextUTF(string filePath)
{
string text = "";
StreamReader reader = File.OpenText(filePath);
while (reader.Peek() != -1)
{
text = reader.ReadToEnd();
}
return text;
}
/// <summary>
/// Response.Write
/// </summary>
/// <param name="msg"></param>
public static void Write(string msg)
{
HttpContext.Current.Response.Write(msg);
}
/// <summary>
/// Write Logs Default path : RootPath/ErrorLogs
/// </summary>
/// <param name="msg"></param>
public static void WriteLogs(string msg)
{
string path = PublicConst.ErrLogPath;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
WriteLogs(msg, path);
}
/// <summary>
/// 寫文字記錄,指定路徑
/// </summary>
/// <param name="logs"></param>
/// <param name="descPath"></param>
/// <returns></returns>
public static string WriteLogs(string logs, string descPath)
{
bool flag;
string text = DateTime.Now.ToString("yyyyMMddHHmmssf");
string path = descPath + @"/" + text + ".txt";
try
{
StreamWriter writer = File.CreateText(path);
writer.Write(logs);
writer.Close();
flag = true;
}
catch
{
flag = false;
}
if (flag)
{
return path;
}
return null;
}
/// <summary>
/// 在頁尾輸出Script片斷;用于繼承自Web.UI.Page的頁面 Default key : addScript
/// </summary>
/// <param name="msg">Script 片斷</param>
/// <param name="page"></param>
public static void WriteScript(string msg, Page page)
{
string script = "<script language='javascript' defer>" + msg + "</script>";
page.RegisterStartupScript("addScript", script);
}
/// <summary>
/// 在頁尾輸出Script片斷;用于繼承自Web.UI.Page的頁面 Default key : addScript
/// </summary>
/// <param name="key"></param>
/// <param name="msg">Script 片斷</param>
/// <param name="page"></param>
public static void WriteScript(string key, string msg, Page page)
{
string script = "<script language='javascript' defer>" + msg + "</script>";
page.RegisterStartupScript(key, script);
}
/// <summary>
/// 輸出文字檔, GB2312格式
/// </summary>
/// <param name="text"></param>
/// <param name="descPath">目標文件完整路徑</param>
public static void WriteText(string text, string descPath)
{
try
{
StreamWriter writer = new StreamWriter(descPath, false, Encoding.GetEncoding("GB2312"));
writer.Write(text);
writer.Close();
}
catch (Exception exception)
{
throw new Exception(exception.Message, exception);
}
}
/// <summary>
/// 輸出文字檔, GB2312格式
/// </summary>
/// <param name="text">輸出內(nèi)容</param>
/// <param name="descDirectory">目錄名</param>
/// <param name="descFileName">文件名</param>
public static void WriteText(string text, string descDirectory, string descFileName)
{
if (!Directory.Exists(descDirectory))
{
Directory.CreateDirectory(descDirectory);
}
WriteText(text, descDirectory + @"/" + descFileName);
}
/// <summary>
/// 輸出文字檔, 按參數(shù)編碼
/// </summary>
/// <param name="text"></param>
/// <param name="descPath">目標文件完整路徑</param>
/// <param name="charSet">GB2312/Big5</param>
public static void WriteTextANSI(string text, string descPath, string charSet)
{
try
{
StreamWriter writer = new StreamWriter(descPath, false, Encoding.GetEncoding(charSet));
writer.Write(text);
writer.Close();
}
catch (Exception exception)
{
throw new Exception(exception.Message, exception);
}
}
#endregion
}
}