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>
/// 构造函数
/// </summary>
public Message()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <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>
/// 显示消息提示对话框,并进行页面跳转
/// </summary>
/// <param name="page">当前页面指针,一般为this </param>
/// <param name="msg">提示信息</param>
/// <param name="url">跳转的目标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调用。 文件名为 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>
/// 根据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>
/// 读取指定路径的文件,按参数编码
/// </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">输出内容</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>
/// 输出文字档, 按参数编码
/// </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
}
}