asp.net url重写
作者:岸上的鱼 日期:2008-04-12
今天研究了一天这个问题
想实现诸如:http://www.ziuziu.cn/2008/2/2
http://www.ziuziu.cn/default.html
等这种url
当然,程序直接生成静态页面可以实现的
但是那样要生成很多这种页面以及文件夹
而且在数据量大的情况下生成是很浪费时间的
在这种情况下就要实现url重写了,当然就很多种方法
下面介绍的是微软的URLRewriter方法
1.首先下载URLRewriter.dll,下载之后直接复制到你的网站的bin目录中
下载地址 http://ziuziu.cn/download/URLRewriter.rar
2.配置你的web.config文件
在<configuration>节点下加
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<!--一下三个是我自己写的测试的,当然你可以写很多个 -->
<!--这个是把http://localhost:2001/default.aspx?id=1 生成http://localhost:2001/d1.aspx 这种页面 -->
<RewriterRule>
<LookFor>~/d(\d{0,5})\.aspx</LookFor>
<SendTo>~/default.aspx?ID=$1</SendTo>
</RewriterRule>
<!--这个是把http://localhost:2001/article.aspx?id=1 生成http://localhost:2001/1/a.html 这种页面 -->
<RewriterRule>
<LookFor>~/(\d{0,5})/a\.html</LookFor>
<SendTo>~/article.aspx?id=$1</SendTo>
</RewriterRule>
<!--这个是把http://localhost:2001/test.aspx?id=1 生成http://localhost:2001/1/ 这种页面 -->
<RewriterRule>
<LookFor>~/(\d{0,5})/</LookFor>
<SendTo>~/test.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
在C#中编写多线程应用程序。
作者:岸上的鱼 日期:2008-04-12
以前在使用VB来实现多线程的时候,发现有一定的难度。虽然也有这样那样的方法,但都不尽人意,但在C#中,要编写多线程应用程序却相当的简单。这篇文章将作简要的介绍,以起到抛砖引玉的作用!
.NET将关于多线程的功能定义在System.Threading名字空间中。因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;)。
即使你没有编写多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个一个的解释。
a.启动线程
顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:
Thread thread1 = new Thread(new ThreadStart( Count));
其中的 Count 是将要被新线程执行的函数。
b.杀死线程
“杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前最好先判断它是否还活着(通过 IsAlive 属性),然后就可以调用 Abort 方法来杀死此线程。
c.暂停线程
它的意思就是让一个正在运行的线程休眠一段时间。如 thread.Sleep(1000); 就是让线程休眠1秒钟。
d.优先级
这个用不着解释了。Thread类中有一个ThreadPriority属性,它用来设置优先级,但不能保证操作系统会接受该优先级。一个线程的优先级可分为5种:Normal, AboveNormal, BelowNormal, Highest, Lowest。具体实现例子如下:
thread.Priority = ThreadPriority.Highest;
e.挂起线程
Thread类的Suspend方法用来挂起线程,知道调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。
if (thread.ThreadState = ThreadState.Running)
{
thread.Suspend();
}
f.恢复线程
用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。
if (thread.ThreadState = ThreadState.Suspended)
{
thread.Resume();
}
下面将列出一个例子,以说明简单的线程处理功能。此例子来自于帮助文档。
using System;
using System.Threading;
// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);
for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
此代码产生的输出类似如下内容:
Main thread: Start a second thread. Main thread: Do some work. ThreadProc: 0 Main thread: Do some work. ThreadProc: 1 Main thread: Do some work. ThreadProc: 2 Main thread: Do some work. ThreadProc: 3 Main thread: Call Join(), to wait until ThreadProc ends. ThreadProc: 4 ThreadProc: 5 ThreadProc: 6 ThreadProc: 7 ThreadProc: 8 ThreadProc: 9 Main thread: ThreadProc.Join has returned. Press Enter to end program.
清明节(4月4日)快到了
作者:岸上的鱼 日期:2008-04-03
"如果你死后,墓志铭打算写点啥?"
1.一居室,求合租,面议。
2.小事招魂,大事挖坟。
3.我觉得我还可以抢救一下!
4.广告位招租
5.提供鞭尸服务,一次100!
6.基因重组中,请稍候二十年
7.单挑冥王哈迪斯中,征求组队!
8.牧师,帮我复活一下下,谢谢。
9.当你看清这行字的时候:朋友,你踩到我了。
10.老子终于不用怕鬼了!
11.给爷笑一个,要不爷给你笑一个?
12.神农氏的墓志铭:我靠!这草有毒!
13.摸骨算命
14.陪聊,提供夜间上门服务。
15.还看,你丫也会有这一天的
16.我从前是个胖子,现在和所有躺著的人一样有骨感。
17.终於可以失掉身体80的水分,可以变瘦了!
18.强力推荐这个给我挖坑的,电话:xxxxxxxxx
19.曾经很黄很暴力,现在很黑很安静
20.谢谢来访,改日登门回拜.呵呵
21.来客请便,无人倒茶,站累躺下一起聊聊?
22.终于解决住房问题了
Global.asax介紹
作者:岸上的鱼 日期:2008-03-23
1.概述
Global.asax 文件继承自HttpApplication 类,它维护一个HttpApplication 对象池,并在需要时将对象池中的对象分配给应用程序。
Global.asax 文件被配置为任何(通过 URL 的)直接 HTTP 请求都被自动拒绝,所以用户不能下载或查看其内容。
ASP.NET 页面框架能够自动识别出对Global.asax 文件所做的任何更改。在 Global.asax 被更改后ASP.NET 页面框架会重新启动应用程序,包括关闭所有的浏览器会话,去除所有状态信息,并重新启动应用程序域。
2.Global.asax 文件包含以下事件:
・ Application_Init:在应用程序被实例化或第一次被调用时,该事件被触发。对于所有的HttpApplication 对象实例,它都会被调用。
・ Application_Disposed:在应用程序被销毁之前触发。这是清除以前所用资源的理想位置。
C#中对注册表的操作
作者:岸上的鱼 日期:2008-03-23
HKEY_CLASSES_ROOT该主键包含了文件的扩展名和应用程序的关联信息以及Window Shell和OLE用于储存注册表的信息。该主键下的子键决定了在WINDOWS中如何显示该类文件以及他们的图标,该主键是从HKEY_LCCAL_MACHINE\SOFTWARE\Classes映射过来的。
HKEY_CURRENT_USER该主键包含了如用户窗口信息,桌面设置等当前用户的信息。
HKEY_LOCAL_MACHINE主键包含了计算机软件和硬件的安装和配置信息,该信息可供所有用户使用
HKEY_USERS该主键记录了当前用户的设置信息,每次用户登入系统时,就会在该主键下生成一个与用户登入名一样的子键,该子键保存了当前用户的桌面设置、背景位图、快捷键,字体等信息。一般应用程序不直接访问改主键,而是通过主键HKEY_CURRENT_USER进行访问。
HKEY_CURRENT_CONFIG该主键保存了计算机当前硬件的配置信息,这些配置可以根据当前所连接的网络类型或硬件驱动软件安装的改变而改变。
1. C#也支持对注册表的编辑,.NET框架在Microsoft.Win32名字空间中提供了两个类来操作注册表:Registry和RegistryKey。这两个类都是密封类不允许被继承。下面我们分别来介绍这两个类。
Registry类提供了7个公共的静态域,分别代表7个基本主键(其中两个在XP系统中没有,在这就不介绍了)分别是:Registry.ClassesRoot,Registry.CurrentUser,Registry.LocalMachine,Registry.Users,Registry.CurrentConfig。它们分别对应哪几个键我想各位一看就会知道吧。
RegistryKey类中提供了对注册表操作的方法。要注意的是操作注册表必须符合系统权限,否则将会抛出错误。
下面是操作注册表常用的几个方法
--1.创建子键的方法原型为:
public RegistryKey CreateSubKey(string sunbkey);
参数sunbkey表示要创建的子键的名称或路径名。创建成功返回被创建的子键,否则返回null。
--2.打开子键的方法原型为:
public RegistryKey OpenSubKey(string name);
public RegistryKey OpenSubKey(string name,bool writable);
参数name表示要打开的子键名或其路径名,参数writable表示被打开的子键是否允许被修改,第一个方法打开的子键是只读的。Microsoft.Win32类还为我们提供了另一个方法,用于打开远程计算机上的注册表,方法原型为:
public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey,string machineName);
--3.删除子键的方法原型为:
public void DeleteKey(string subkey);
该方法用于删除指定的主键。如果要删除的子键还包含主键则删除失败,并返回一个异常,如果要彻底删除该子键极其目录下的子键可以用方法DeleteSubKeyTree,该方法原型如下:
public void DeleteKeyTree(string subkey);
--4.读取键值的方法原型如下:
public object GetValue(string name);
public object GetValue(string name,object defaultValue);
参数name表示键的名称,返回类型是一个object类型,如果指定的键不存在则返回null。如果失败又不希望返回的值是null则可以指定参数defaultValue,指定了参数则在读取失败的情况下返回该参数指定的值。
C#调用外部dos命令并取得返回結果
作者:岸上的鱼 日期:2008-03-23
private static string CmdPing(string strIp)
{
Process p = new Process();
//设定程序名
p.StartInfo.FileName = "cmd.exe";
//关闭Shell的使用
p.StartInfo.UseShellExecute = false;
//重定向标准输入
p.StartInfo.RedirectStandardInput = true;
//重定向标准输出
p.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
p.StartInfo.RedirectStandardError = true;
//设置不显示窗口
p.StartInfo.CreateNoWindow = true;
string pingrst;
//启动进程
p.Start();
//输入要执行的命令,这里就是ping
p.StandardInput.WriteLine("ping -n 1 " + strIp);
p.StandardInput.WriteLine("exit");
//从输出流获取命令执行结果
string strRst = p.StandardOutput.ReadToEnd();
//分析strRst字符串就可以知道网络的连接情况
if (strRst.IndexOf("(0% loss)") != -1)
pingrst = "连接";
else if (strRst.IndexOf("Destination host unreachable.") != -1)
pingrst = "无法到达目的主机";
else if (strRst.IndexOf("Request timed out.") != -1)
pingrst = "超时";
else if (strRst.IndexOf("Unknown host") != -1)
pingrst = "无法解析主机";
else
pingrst = strRst;
p.Close();
return pingrst;
}
HOOK概念,运行机制及类型
作者:岸上的鱼 日期:2008-03-23
LRESULT CALLBACK HookProc( int nCode, WPARAM wParam, LPARAM lParam);
HHOOK SetWindowsHookEx( int idHook, // 钩子的类型,即它处理的消息类型 HOOKPROC lpfn, // 钩子子程的地址指针。如果dwThreadId参数为0 // 或是一个由别的进程创建的线程的标识, // lpfn必须指向DLL中的钩子子程。 // 除此以外,lpfn可以指向当前进程的一段钩子子程代码。 // 钩子函数的入口地址,当钩子钩到任何消息后便调用这个函数。 HINSTANCE hMod, // 应用程序实例的句柄。标识包含lpfn所指的子程的DLL。 // 如果dwThreadId 标识当前进程创建的一个线程, // 而且子程代码位于当前进程,hMod必须为NULL。 // 可以很简单的设定其为本应用程序的实例句柄。 DWORD dwThreadId // 与安装的钩子子程相关联的线程的标识符。 // 如果为0,钩子子程与所有的线程关联,即为全局钩子。 );
LRESULT CallNextHookEx ( HHOOK hhk; int nCode; WPARAM wParam; LPARAM lParam; );
UnHookWindowsHookEx( HHOOK hhk;);
#pragma data_seg("SharedDataName")HHOOK hHook=NULL;#pragma data_seg()
本文轉自: http://www.microsoft.com/china/community/program/originalarticles/techdoc/hook.mspx