DOTNET

C#读取Word的内容

下了一个电子书,里面居然全部都是word格式的文件,一点都不便于阅读,连个目录都不好翻,只好把它读出来,弄成html格式的。下面是读取word内容的代码,比较简单了。

using System;
using System.IO;
using System.Reflection;
using Word;   

//----------------------------------------------------------------------
static string WordReader( string path )
{
	string WordContent = "";

	Word.Application app = new ApplicationClass();

	object fileName = path;
	object optional = Missing.Value;
	object visible = true;
	if( File.Exists( fileName.ToString() ) )
	{
		Word.Document doc = app.Documents.Open(
		 ref fileName,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref visible,
		 ref optional,
		 ref optional,
		 ref optional,
		 ref optional );

		WordContent = doc.Content.Text;

		object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
		object originalFormat = Missing.Value;
		object routeDocument = Missing.Value;
		app.Quit( ref saveChanges, ref originalFormat, ref routeDocument );
	}
	else
	{
		return string.Empty;
	}

	return WordContent;

}

工程中引用“Microsoft Word 11.0 object library”的Microsoft COM组件。该组件提供的类和方法来读取Word文档

在WebBrowser中用SendMessage模拟鼠标点击

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace BrowserMouseClick
{
  public partial class Form1 : Form
  {
	  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
	  static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

	  [DllImport("user32.dll", SetLastError = true)]
	  static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

	  [DllImport("user32.dll", CharSet = CharSet.Auto)]
	  static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

	  public Form1()
	  {
		  InitializeComponent();
	  }

	  private void Form1_Load(object sender, EventArgs e)
	  {
		  webBrowser1.Navigate("http://www.devpub.com");
	  }

	  private void btnMouseClick_Click(object sender, EventArgs e)
	  {
		  int x = 100; // X coordinate of the click
		  int y = 80; // Y coordinate of the click
		  IntPtr handle = webBrowser1.Handle;
		  StringBuilder className = new StringBuilder(100);
		  while (className.ToString() != "Internet Explorer_Server") // The class control for the browser
		  {
			  handle = GetWindow(handle, 5); // Get a handle to the child window
			  GetClassName(handle, className, className.Capacity);
		  }

		  IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
		  IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
		  const uint downCode = 0x201; // Left click down code
		  const uint upCode = 0x202; // Left click up code
		  SendMessage(handle, downCode, wParam, lParam); // Mouse button down
		  SendMessage(handle, upCode, wParam, lParam); // Mouse button up
	  }
  }
}

想在WebBrowser控件里面模拟鼠标点击,在百度上找了半天,怎么也找不到,还是google强大,在一个国外网站上找到的,代码比较清楚了,不做说明。

用SendMessage在不同程序间发送消息

窗体1中的代码:

	/////////////////////////////////////////
	///file name: Note.cs
	///
	public class Note
	{
		//声明 API 函数
		[DllImport( "User32.dll", EntryPoint = "SendMessage" )]
		private static extern int SendMessage(
			  int hWnd,     // handle to destination window
			  int Msg,     // message
			  int wParam,   // first message parameter
			  int lParam   // second message parameter
		);
		[DllImport( "User32.dll", EntryPoint = "FindWindow" )]
		private static extern int FindWindow( string lpClassName, string lpWindowName );
		//定义消息常数
		public const int USER = 0x500;
		public const int TEST = USER + 1;

		//向窗体发送消息的函数
		private void SendMsgToMainForm( int MSG )
		{
			int WINDOW_HANDLER = FindWindow( null, @"Note Pad" );
			if( WINDOW_HANDLER == 0 )
			{
				throw new Exception( "Could not find Main window!" );
			}
			SendMessage( WINDOW_HANDLER, MSG, 100, 200 );
		}
	}

Read More »

C#判断两个日期是否在同一周,某日期是本月的第几周

判断两个日期是否在同一周

/// <summary>
/// 判断两个日期是否在同一周
/// </summary>
/// <param name="dtmS">开始日期</param>
/// <param name="dtmE">结束日期</param>
/// <returns></returns>
private bool IsInSameWeek(DateTime dtmS, DateTime dtmE)
{
	TimeSpan ts=dtmE - dtmS;
	double dbl=ts.TotalDays;
	int intDow=Convert.ToInt32(dtmE.DayOfWeek);
	if(intDow==0)
		intDow=7;

	if(dbl>=7 || dbl>=intDow)
		return false;
	else
		return true;
}

Read More »

使用ISAPI_Rewrite重写URL后取页面的访问地址

在IIS里面装了ISAPI_Rewrite后,重写URL后在程序里面用Request.RawUrl和Requst.Url取得的地址都是没有经过重写的URL,想要取得重写过的URL必须这么做:在重写的RewriteRule后面[]里面的参数必须带上U,Request.ServerVeriables["HTTP_X_REWRITE_URL"],即可取得页面的重写过的URL。

另附上在php中的取法:

IIS中

$_SERVER['HTTP_X_REWRITE_URL']

Apache使用:

$_SERVER['REDIRECT_QUERY_STRING']或$_SERVER['REDIRECT_URL']

Response.Status和Response.StatusDescription的区别

看VS里面的提示,怎么都看不出来这两个有什么区别,反正都是字符串。
其实它们之间的区别就是Status的值里面比StatusDescription多一个状态码,如下:

Response.Status = “304 Not Modified”;
Response.StatusDescription = “Not Modified”;

正则表达式之获取匹配、非获取匹配、正向预查、负向预查

看一下下面这个表格,就应该很清楚了,绿色代表匹配结果。

说明 正则表达式 匹配结果 $1
普通表达式 windows 98|2000|2003 windows 98 windows 2000 windows 2003
后向引用,获取匹配 windows (98|2000|2003) windows 98windows 2000windows 2003 98,2000,2003
非获取匹配 windows (?:98|2000|2003) windows 98windows 2000windows 2003 获取不到$1
正向预查,非获取匹配 windows (?=98|2000) windows 98 windows 2000 windows 2003
负向预查,非获取匹配 windows (?!98|2000) windows 98 windows 2000 windows 2003

.NET源码加密保护详解|.NET程序加密(转载)

一. 前言

大家好,我是康杰,大家可以叫我Jason

我和大家一样,都是搞技术出身,也未当过讲师,所以口材有限,如果讲得不好之处,还希望大家多多海含,谢谢。

今天是我们第一次见面,能认识你们,真的很高兴。

下面我们不要耽误大家的宝贵时间,让我们马上开始上课吧。

DotNet ms开发并推广的企业解决方案,也是Ms以后几年的核心发展战略之一,所以我觉得 DotNet 是有前途的,他有一个优秀的概念,还有一个强大的财团,想失败都很难啊。DotNet 缺乏的是大型企业高层管理人员对它的信心,这还需要时间和事例去证明,世界上待开发的大案件还很多,Java DotNet 最终谁的市场比例多,现在还说不清楚。

二. 简介DOTNET 编译原理

相信大家都使用过 Dotnet ,可能还有不少高手。不过我还要讲讲Dotnet的基础知识,Dotnet的编译原理。

Read More »

IIS6下运行MVC项目,请求链接带扩展名.aspx

想用MVC做个网站玩玩,MVC默认项目的链接形式是不带扩展名的,但服务器的环境是.net2.0和iis6,想用没有扩展名的请求路径形式,需要加通配符映射,这样就会把网站的所有文件都交给了asp.net处理,有点浪费资源,想想还是放弃了,还是加上.aspx的扩展名,服务器上再用isapi_rewrite重写算了,完全可以实现相同的效果。当我把global.asxa.cs里面的routes.MapRoute改为如下时

routes.MapRoute(
	&amp;quot;Default&amp;quot;, // Route name
	&amp;quot;{controller}.aspx/{action}/{id}&amp;quot;, // URL with parameters
	new { controller = &amp;quot;Home&amp;quot;, action = &amp;quot;Index&amp;quot;, id = &amp;quot;&amp;quot; } // Parameter defaults
);

访问首页是总是出现“传入的请求不与任何路由匹配。”的错误,访问首页时Request.Path=”/”,Request.Path与{controller}.aspx/{action}/{id}确实不匹配,但是为什么我没有加.aspx的时候Request.Path就可以匹配{controller}/{action}/{id}它呢?{}里面的东西没有传值的话可以忽略,但还有两个/,怎么就能匹配了?有时间去看看内部的实现代码,现在也不想管那么多了。加了一个MapRoute解决问题。

routes.MapRoute(
	&amp;quot;DefaultPage&amp;quot;, // Route name
	&amp;quot;&amp;quot;, // 新加一个链接匹配字符串,为空就行了
	new { controller = &amp;quot;Home&amp;quot;, action = &amp;quot;Index&amp;quot;, id = &amp;quot;&amp;quot; } // Parameter defaults
);

routes.MapRoute(
	&amp;quot;Default&amp;quot;, // Route name
	&amp;quot;{controller}.aspx/{action}/{id}&amp;quot;, // URL with parameters
	new { controller = &amp;quot;Home&amp;quot;, action = &amp;quot;Index&amp;quot;, id = &amp;quot;&amp;quot; } // Parameter defaults
);

让Window程序只能运行一个实例

如何让window运用程序只有一个实例,重复运行即会显示以前打开的实例。

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;   

namespace Junstyle
{
    static class Program
    {
        /// &amp;lt;summary&amp;gt;
        /// 应用程序的主入口点。
        /// &amp;lt;/summary&amp;gt;
        [STAThread]
        static void Main()
        {
            if( Exist() )
            {
                Application.Exit();
                return;
            }   

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault( false );
            Application.Run( new frmMain() );
        }   

        [DllImport( &amp;quot;User32.dll&amp;quot; )]
        private static extern bool ShowWindowAsync( System.IntPtr hWnd, int cmdShow );
        [DllImport( &amp;quot;User32.dll&amp;quot; )]
        private static extern bool SetForegroundWindow( System.IntPtr hWnd );   

        public static bool Exist()
        {
            bool ret = false;
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName( current.ProcessName );
            //遍历与当前进程名称相同的进程列表
            foreach( Process process in processes )
            {
                //Ignore the current process
                if( process.Id != current.Id )
                {   

                    if( Assembly.GetExecutingAssembly().Location.Replace( &amp;quot;/&amp;quot;, &amp;quot;\\&amp;quot; ) == current.MainModule.FileName )
                    {
                        ShowWindowAsync( process.MainWindowHandle, 1 ); //调用api函数,正常显示窗口
                        SetForegroundWindow( process.MainWindowHandle ); //将窗口放置最前端。
                        return true;
                    }
                }
            }
            return ret;
        }
    }
}