Message类,常用方法类(C#)

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

 }
}
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中SendMessage和PostMessage的参数传递 在C#中可以使用Window API提供的SendMessage和PostMessage来传递参数。两者的区别简单介绍下:返回值的不同,我们先看一下 MSDN 里的声明: LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); BOOL PostMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); 其中 4 个参数的意义是一样的,返回值型不同(其实从数据上看他们一样是一个 32 位的数,只是意义不一样),LRESULT 表示的是消息被处理后的返回值,BOOL 表示的是消息是不是 Post 成功。 2、PostMessage 是异步的,SendMessage 是同步的。 PostMessage 只把消息放入队列,不管消息是否被处理就返回,消息可能不被处理;而 SendMessage 等待消息被处理完了之后才返回,如果消息不被处理,发送消息的线程将一直被阻塞。 3、如果在同一个线程内,SendMessage 发送消息时,由 USER32.DLL 模块调用目标窗口的消息处理程序,并将结果返回。SendMessage 在同一线程中发送消息并不入线程消息队列。PostMessage 发送消息时,消息要先放入线程的消息队列,然后通过消息循环分派到目标窗口(DispatchMessage)。 如果在不同线程内,SendMessage 发送消息到目标窗口所属线程的消息队列,然后发送消息的线程在 USER32.DLL 模块内监视和等待消息处理,直到目标窗口处理完返回。SendMessage 在返回前还做了很多工作,比如,响应别的线程向它 SendMessage。Post 到别的线程时,最好用 PostThreadMessage 代替 PostMessage,PostMessage 的 hWnd 参数可以是 NULL,等效于 PostThreadMessage + GetCurrentThreadId。Post WM_QUIT 时,应使用 PostQuitMessage 代替。 4、系统只整编(marshal)系统消息(0 到 WM_USER 之间的消息),发送用户消息(WM_USER 以上)到别的进程时,需要自己做整编。 用 PostMessage、SendNotifyMessage、SendMessageCallback 等异步函数发送系统消息时,参数里不可以使用指针,因为发送者并不等待消息的处理就返回,接受者还没处理指针就已经被释放了。 5、在 Windows 2000/XP 里,每个消息队列最多只能存放 10,000 个 Post 的消息,超过的还没被处理的将不会被处理,直接丢掉。这个值可以改得更大:[HKEY_LOCAL_MACHINE/SOFTWARE/ Microsoft/Windows NT/CurrentVersion/Windows] USERPostMessageLimit,最小可以是 4000。 PostMessage只负责将消息放到消息队列中,不确定何时及是否处理 SendMessage要等到受到消息处理的返回码(DWord型)后才继续 PostMessage执行后马上返回 SendMessage必须等到消息被处理后才会返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rjzou2006

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值