
2009年1月14日
为响应朋友的邀请,写了一个关于“云安全”的小故事。纯粹娱乐。拿出来供大家把玩。
月圆之夜,紫禁之巅;一剑西来,天外飞仙。
霞光映照,彩蝶翩翩;邀约高手,改地决战。
光华映照,彩云之端;绝世高手,决战在前。
一剑直刺,侧身斜蹿;转身回马,连环空翻。
你来我往,你去我还;光怪陆离,浪起云翻。
高手决战,四十九天;胜负难判,高低难参。
停手罢斗,握手言欢;虽曰决战,未想伤焉。
若非云顶,岂能相安;携手高呼,曰云安全。
posted @ 2009-01-14 12:41 Gofficer 阅读(85) 评论(1)
编辑

2008年11月28日
彭端淑(清)
天下事有难易乎?为之,则难者亦易矣;不为,则易者亦难矣。人之为学有难易乎?学之,则难者亦易矣;不学,则易者亦难矣。
吾资之昏,不逮人也;吾材之庸,不逮人也。旦旦而学之,久而不怠焉,迄乎成,而亦不知其昏与庸也。吾资之聪,倍人也;吾材之敏,倍人也;屏弃而不用,其与昏与庸无以异也。圣人之道,卒于鲁也传之。然则昏庸聪敏之用,岂有常哉?
蜀之鄙有二僧,其一贫,其一富。贫者语于富者曰:“吾欲之南海,何如?”富者曰:“子何恃而往?”曰:“吾一瓶一钵足矣。”富者曰:“吾数年来欲买舟而下,犹未能也。子何恃而往?”越明年,贫者自南海还,以告富者。富者有惭色。
西蜀之去南海,不知几千里也,僧富者不能至而贫者至焉。人之立志,顾不如蜀鄙之僧哉?是故聪与敏,可恃而不可恃也;自恃其聪与敏而不学者,自败者也。昏与庸,可限而不可限也;不自限其昏与庸而力学不倦者,自力者也。
posted @ 2008-11-28 09:27 Gofficer 阅读(100) 评论(0)
编辑

2008年10月30日
网页自动登录(提交Post内容)的用途很多,如验证身份、程序升级、网络投票等,以下是用C#实现的方法。
网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser、WebClient、HttpWebRequest这三个。以下就分别用这三种方法来实现:
1、WebBrowser是个"迷你"浏览器,其特点是Post时不用关心Cookie、内置JS等问题
WebBrowser是VS2005新提供的组件(其实就是封装了IE接口),实现POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 来实现,代码如下:
|
HtmlElement ClickBtn =null;
if (e.Url.ToString().ToLower().IndexOf("xxx.htm") > 0) //登陆页面
{
HtmlDocument doc = webBrowser1.Document;
for (int i = 0; i < doc.All.Count ; i++)
{
if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
{
switch (doc.All[i].Name)
{
case "userCtl":
doc.All[i].InnerText = "user01";
break;
case "passCt1":
doc.All[i].InnerText = "mypass";
break;
case "B1":
ClickBtn = doc.All[i]; //提交按钮
break;
}
}
}
ClickBtn.InvokeMember("Click"); //执行按扭操作
}
|
2、WebClient封装了HTTP的一些类,操作简单,相较于webBrowser,特点是可以自设代理,缺点是对COOKIE的控制
WebClient的运行全在后台,并且提供了异步操作的能力,这样很方便并发多个任务,然后等待结果的返回,再逐个处理。多任务异步调用的代码如下:
|
private void StartLoop(int ProxyNum)
{
WebClient [] wcArray = new WebClient[ProxyNum]; //初始化
for (int idArray = 0; idArray< ProxyNum;idArray++)
{
wcArray[idArray] = new WebClient();
wcArray[idArray].OpenReadCompleted += new OpenReadCompletedEventHandler(Pic_OpenReadCompleted2);
wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEventHandler(Pic_UploadDataCompleted2);
try
{
......
wcArray[idArray].Proxy = new WebProxy(proxy[1], port);
wcArray[idArray].OpenReadAsync(new Uri("http://xxxx.com.cn/tp.asp?Id=129")); //打开WEB;
proxy = null;
}
catch
{
}
}
}
private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
string textData = new StreamReader(e.Result, Encoding.Default).ReadToEnd(); //取返回信息
.....
String cookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
((WebClient)sender).Headers.Add("Content-Type", "application/x-www-form-urlencoded");
((WebClient)sender).Headers.Add("Accept-Language", "zh-cn");
((WebClient)sender).Headers.Add("Cookie", cookie);
string postData = "......"
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化成二进制数组
((WebClient)sender).UploadDataAsync(new Uri("http://xxxxxxy.com.cn/tp.asp?Id=129"), "POST", byteArray);
}
}
private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEventArgs e)
{
if (e.Error == null)
{
string returnMessage = Encoding.Default.GetString(e.Result);
......
}
}
|
3、HttpWebRequest较为低层,能实现的功能较多,Cookie操作也很简单
|
private bool PostWebRequest()
{
CookieContainer cc = new CookieContainer();
string pos tData = "user=" + strUser + "&pass=" + strPsd;
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("http://www.xxxx.com/chk.asp"));
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
......
}
|
HttpWebRequest同样提供了异步操作,有兴趣的朋友自己查MSDN,实现起来也不难。
网络转载
posted @ 2008-10-30 11:24 Gofficer 阅读(677) 评论(0)
编辑
2008-05-21 07:00 作者: 肖波 出处: 天极网
C# 调用外部进程的类,网上可以搜出很多来,为什么要再写一遍,实在是因为最近从网上拷贝了一个简单的例程用到项目中,运行有问题,后来研究了半天,才解决了这些问题。于是打算重写,一来说说调用一个外部进程这么简单的一件事究竟会有哪些问题,二来也希望我写的这个相对比较完整的类可以为软件开发的同道们节约一些脑细胞,以便集中优势兵力解决那些真正高深复杂的软件问题。
在开始正题之前,我们先来看一看网上比较常见的执行外部进程的函数
private string RunCmd(string command)
{
//例Process
Process p = new Process();
p.StartInfo.FileName = "cmd.exe"; //确定程序名
p.StartInfo.Arguments = "/c " + command; //确定程式命令行
p.StartInfo.UseShellExecute = false; //Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向输入
p.StartInfo.RedirectStandardOutput = true; //重定向输出
p.StartInfo.RedirectStandardError = true; //重定向输出错误
p.StartInfo.CreateNoWindow = true; //设置置不显示示窗口
p.Start(); //00
//p.StandardInput.WriteLine(command); //也可以用这种方式输入入要行的命令
//p.StandardInput.WriteLine("exit"); //要得加上Exit要不然下一行程式
return p.StandardOutput.ReadToEnd(); //输出出流取得命令行结果
} |
这个方法应该是比较常见的调用外部进程的方法,我以前也一直是这样调用外部进程的,也没有碰到过什么问题。但这次调用的外部进程比较特殊,用这种方法调用就出现了两个问题。
第一个问题是这个被调用的外部进程有时候会出现异常,出现异常后Windows会弹出错误报告框,程序于是吊死在那里,必须手工干预。这个问题比较好解决,程序中设置一下注册表搞定。
第二个问题是调用这个外部进程(是一个控制台进程)后,程序会阻塞在p.StandardOutput.ReadToEnd();这一句,永远无法出来,被调用的那个控制台程序也被吊死。但该控制台进程在CMD 中是可以正常执行的。后来看来一些资料才发现原来原因是出在该控制台程序控制台输出大量字符串,管道重定向后,调用程序没有及时将管道中的输出数据取出,结果导致管道被阻塞,程序吊死。在这里还有另外一个问题,虽然这次没有遇到,但网上有其他人遇到,就是错误信息管道不及时取出数据,也会被阻塞,而且如果要同时取出两个管道的数据,必须要利用一个辅助线程才能实现。
问题讲完了,下面给出这个类的完整代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace Laboratory.Process
{
class ReadErrorThread
{
System.Threading.Thread m_Thread;
System.Diagnostics.Process m_Process;
String m_Error;
bool m_HasExisted;
object m_LockObj = new object();
public String Error
{
get
{
return m_Error;
}
}
public bool HasExisted
{
get
{
lock (m_LockObj)
{
return m_HasExisted;
}
}
set
{
lock (m_LockObj)
{
m_HasExisted = value;
}
}
}
private void ReadError()
{
StringBuilder strError = new StringBuilder();
while (!m_Process.HasExited)
{
strError.Append(m_Process.StandardError.ReadLine());
}
strError.Append(m_Process.StandardError.ReadToEnd());
m_Error = strError.ToString();
HasExisted = true;
}
public ReadErrorThread(System.Diagnostics.Process p)
{
HasExisted = false;
m_Error = "";
m_Process = p;
m_Thread = new Thread(new ThreadStart(ReadError));
m_Thread.Start();
}
}
class RunProcess
{
private String m_Error;
private String m_Output;
public String Error
{
get
{
return m_Error;
}
}
public String Output
{
get
{
return m_Output;
}
}
public bool HasError
{
get
{
return m_Error != "" && m_Error != null;
}
}
public void Run(String fileName, String para)
{
StringBuilder outputStr = new StringBuilder();
try
{
//disable the error report dialog.
//reference: http://www.devcow.com/blogs/adnrg/archive/2006/07/14/Disable-Error-Reporting-Dialog-for-your-application-with-the-registry.aspx
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"software\microsoft\PCHealth\ErrorReporting\", true);
int doReport = (int)key.GetValue("DoReport");
if (doReport != 0)
{
key.SetValue("DoReport", 0);
}
int showUI = (int)key.GetValue("ShowUI");
if (showUI != 0)
{
key.SetValue("ShowUI", 0);
}
}
catch
{
}
m_Error = "";
m_Output = "";
try
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = fileName;
p.StartInfo.Arguments = para;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
ReadErrorThread readErrorThread = new ReadErrorThread(p);
while (!p.HasExited)
{
outputStr.Append(p.StandardOutput.ReadLine()+"\r\n");
}
outputStr.Append(p.StandardOutput.ReadToEnd());
while (!readErrorThread.HasExisted)
{
Thread.Sleep(1);
}
m_Error = readErrorThread.Error;
m_Output = outputStr.ToString();
}
catch (Exception e)
{
m_Error = e.Message;
}
}
}
}
|
posted @ 2008-10-30 11:12 Gofficer 阅读(107) 评论(0)
编辑

2008年10月16日
概要
快速实现在Windows应用程序中支持拖拽的TreeView控件。
技术描述
TreeView已经提供了支持拖拽的事件:
•
ItemDrag: 用户拖拽TreeNode时触发。当它发生时,调用DoDragDrop方法初始化拖拽过程。
•
DragEnter: 在你初始化拖拽操作后,你必须处理目标TreeView控件的DragEnter事件。 这个事件发生在用户拖拽TreeNode对象从TreeView控件到目标控件范围点内。DragEnter事件能够指定目标TreeView控件,无论拖拽操作对这个控件是否用。代码中仅仅是移动操作。
•
DragDrop: 最后是要处理目标TreeView的DragDrop事件。这个事件发生在用户推拽TreeNode对象并释放到目标控件中。处理这个事件, 返回TreeNode对象并添加到目标TreeView控件上。代码中用Data对象返回。
以下代码实现一个TreeView控件上拖拽任意节点到指定节点上,也可以自己扩展为多个TreeView控件间TreeNode相互拖拽的程序。Data对象的GetData方法返回被拖拽的TreeNode对象。GetNodeAt方法用来确定这个TreeNode对象拖拽到的目标控件(这里就是目标TreeNode对象)。在确定位置之后把源TreeNode对象添加到目标TreeNode对象下面,作为其子结点。因为是移动操作,因此最后会把源TreeNode对象删除掉。
通过以下几步创建实例程序:
1.
创建C# Windows应用程序
2.
界面增加一个TreeView控件
3.
设置TreeView的AllowDrop属性设置为True
4.
Page_Load方法中增加如下代码:
private void Form1_Load(object sender, System.EventArgs e)
{
// TreeView控件增加一些测试节点
TreeNode ParentNode1;
ParentNode1 = treeView1.Nodes.Add("tv1");
ParentNode1.Nodes.Add("tv1FirstChild");
ParentNode1.Nodes.Add("tv1SecondChild");
ParentNode1.Nodes.Add("tv1ThirdChild");
ParentNode1.Nodes.Add("tv1FourthChild");
ParentNode1.Expand();
// TreeView控件增加事件
this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView_ItemDrag);
this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView_DragEnter);
this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView_DragDrop);
}
5.
//treeView_ItemDrag 事件代码:
private void treeView_ItemDrag(object sender,
System.Windows.Forms.ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}
6.
// treeView_DragEnter事件代码:
private void treeView_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
7.
// treeView_DragDrop事件代码:
private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeNode NewNode;
if(e.Data.GetDataPresent(typeof(TreeNode)))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
DestinationNode.Nodes.Add((TreeNode) NewNode.Clone());
DestinationNode.Expand();
//删除已经移动的节点
NewNode.Remove();
}
}
posted @ 2008-10-16 09:52 Gofficer 阅读(521) 评论(2)
编辑

2008年10月15日
在网上找到的一段代码,经测试可以使用,不过需要已经安装了office的ppt软件。
下面吧代码拿出来和大家分享:

Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
namespace testPPTtoJPG
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ApplicationClass ac = new ApplicationClass();
string srcRoot;
string dstRoot;
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Select root folder";
if (fbd.ShowDialog() != DialogResult.OK)
return;
srcRoot = fbd.SelectedPath;
fbd.Description = "Select destination folder";
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() != DialogResult.OK)
return;
dstRoot = fbd.SelectedPath;
string[] pptFiles = Directory.GetFiles(srcRoot, "*.ppt", SearchOption.AllDirectories);
string[] potFiles = Directory.GetFiles(srcRoot, "*.pot", SearchOption.AllDirectories);
string[] pptxFiles = Directory.GetFiles(srcRoot, "*.pptx", SearchOption.AllDirectories);
Console.WriteLine("Find " + (
pptxFiles.GetUpperBound(0) + 1 +
potFiles.GetUpperBound(0) + 1 +
pptxFiles.GetUpperBound(0) + 1) + "Files");
foreach (string ppt in pptFiles)
{
string dir = ppt.Substring(srcRoot.Length + 1);
string fullDir = Path.Combine(dstRoot, dir);
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting PPT File (" + fullDir + ")");
Presentation p = ac.Presentations.Open(ppt, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
foreach (string pot in potFiles)
{
string dir = pot.Substring(srcRoot.Length + 1);
string fullDir = Path.Combine(dstRoot, dir);
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting POT File (" + fullDir + ")");
Presentation p = ac.Presentations.Open(pot, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
foreach (string pptx in pptxFiles)
{
string dir = pptx.Substring(srcRoot.Length + 1);
string fullDir = Path.Combine(dstRoot, Path.GetFileName(dir));
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting PPTX File (" + fullDir + ")");
Presentation p = ac.Presentations.Open2007(pptx, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); ;
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
Console.WriteLine("All done, press enter key to exit");
Console.ReadLine();
}
}
}
posted @ 2008-10-15 17:47 Gofficer 阅读(1246) 评论(0)
编辑

2008年1月15日
摘要: 最近又听到有人唠叨。不知道开发人员的未来在哪里。 我来说两句,也欢迎大家一起讨论。 一般来讲,开发人员,随着能力的提升,可以成为软件架构师,再高就是CIO或者CTO。当然,你也可以选择自己当老板。 还有另外一种选择,那就是做一名资深程序员。 实际上,有很多程序员,就一直在做程序开发,做几十年,做一辈子。资深程序员也可以有很高的收入,有时可能高于软件架构师、项目经理。因此,成为一名资深的开发技术专家...
阅读全文
posted @ 2008-01-15 11:21 Gofficer 阅读(206) 评论(3)
编辑

2008年1月10日
摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> using System.Runtime.InteropServices;[Flags]publicenumExitWindows:uint{LogOff=0x00,//注销ShutDown=0x01,...
阅读全文
posted @ 2008-01-10 11:12 Gofficer 阅读(1513) 评论(9)
编辑

2008年1月9日
摘要: 有一个备注的内容需要在界面上显示,由于备注内容可能比较多,单行的单元格往往显示不全,所以希望能够多行显示,以便用户查看。实现换行显示需要如下代码:ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].CellMultiLine=Infragistics.Win.DefaultableBoolean.True;ultraGrid1.DisplayLayout...
阅读全文
posted @ 2008-01-09 16:34 Gofficer 阅读(396) 评论(0)
编辑
摘要: 因为id可能不是连续的,所以不能用取得10<id<20的记录的方法。有三种方法可以实现:一、搜索前20条记录,指定不包括前10条语句:select top 20 * from tbl where id not in (select top 10 id from tbl)二、搜索记录生成临时表,建立临时表的自增id。通过取得自增id的10<id<20的记录的方法取得所需数据语...
阅读全文
posted @ 2008-01-09 09:32 Gofficer 阅读(1058) 评论(12)
编辑