18、bytes[]和int的相互转换
int s = 100;
byte[] shi = System.BitConverter.GetBytes(s);
int sh = System.BitConverter.ToInt32(shi,0);
19、BitArray和int的相互转换
int[] a =
new
int[1];
a[0] = 100;
int[] b =
new
int[1];
System.Collections.BitArray shit =
new BitArray(a);
shit.CopyTo(b,0);
20、随机数:
1) System.Random
2) xdev所提到的System.Security.Cryptography.RNGCryptoServiceProvider
3) 直接调用Platform SDK中的CryptGenRandom()函数。
其中,和1)相比,2)和3)可以被作为真正的随机数来使用。
21、最简单的实现双缓冲绘图:
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();
还有一种方法,是先建一个bmp,在该bmp上绘制完毕后,最后一次性复制到目标画布上。但在实际使用中,我发现该方法有时反倒闪烁得厉害,不知是为什么,郁闷~
22、取得局域网内本机IP地址
String name = Dns.GetHostName();
IPHostEntry ips = Dns.GetHostByName(name);
return ips.AddressList[0].ToString();
23、取得局域网广播地址
IPv4广播地址的定义是网络号码与网络掩码逻辑“非”之间的逻辑“或”。
例如一个地址为:197.8.43.211,其子网掩码为255.255.255.240则其网络号码为两者的二进制逻辑"与",即197.8.43.208 网络掩码的逻辑"非"为0.0.0.15,与网络号码197.8.43.208二进制逻辑"或"的结果为197.8.43.223,即该子网的广播地址.
24、Asp.net直接下载文件
FileStream fileStream=
new FileStream(pFileName,FileMode.Open)
long fileSize = fileStream.Length;
Context.Response.ContentType="application/octet-stream";
Context.Response.AddHeader("Content-Disposition","attachment; filename=\"" + fileName + "\";");
Context.Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer=
new
byte[fileSize];
fileStream.Read(fileBuffer, 0, (
int)fileSize);
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
25、十进制数转成十六进制字符串
int num =123;
string str = Convert.ToString(num,16);
十六进制字符串转十进制数
string str = "7b";
int num = Convert.ToInt32(str,16);
其它进制也是一样,只要把16改进2或8或10就可以了.
26、在网页上实现html编辑的最简单代码(来自微软网站)
<
html
>
<
head
>
<
title
>DHTML 编辑控件代理示例
</
title
>
<
script
type
="text/javascript"
>
function
FillEditor() {
var
doc
=
editor.document;
doc.designMode
=
"
on
"
;
doc.write(
"
<body><p><i>可视化</i> <u>安静</u></p></body>
"
);
doc.close();
}
</
script
>
</
head
>
<
body
onload
="FillEditor()"
>
<
iframe
id
="editor"
scrolling
="yes"
height
="100"
width
="300"
>
</
iframe
>
<
p
>
<
input
type
=button
value
="Submit"
onclick
=
"alert(editor.document.getElementsByTagName('HTML')[0].outerHTML)"
>
</
body
>
</
html
>
此页面显示了包括格式化 HTML 的可编辑区域。您可在此区域中输入内容,也可使用热键,如 CTRL+I 切换斜体,CTRL+U 切换下划线。当按下 Submit 按钮时,出现一个消息框,显示 <iframe> 元素的 HTML 内容。要等效地实现 DHTML 编辑控件的属性、方法和事件,必须要使用脚本。
出于安全考虑,只有来自相同域的内容才可跨过 <iframe> 界限进行访问。要确保框架的内容可被访问,必须使用 src 属性,从同一域的 URL 中对其进行初始化,正如下列示例所示:
<iframe id="editor"
scrolling="yes"
height="100"
width="300"
src="templates/blank.htm">
</iframe>
有关在 Internet Explorer 中使用 designMode 属性进行编辑的详细信息,请参阅 Microsoft 网站上的 Introduction to MSHTML Editing(英文)页面。
27、强制类型转换与as类型转换的区别
当类型转换非法时,强制类型转换将抛出一个System.InvalidCastException异常,而as不会抛出异常,它返回一个null值。
28、FromBase64String(string s)函数,对s有如下要求,否则会报错“Base-64字符中的无效字符”:
s 由基 64 数字、空白字符和尾随填充字符组成。从零开始以升序排列的以 64 为基的数字为大写字符“A”到“Z”、小写字符“a”到“z”、数字“0”到“9”以及符号“+”和“/”。 空白字符为 Tab、空格、回车和换行。s 中可以出现任意数目的空白字符,因为所有空白字符都将被忽略。 无值字符“=”用于尾部的空白。s 的末尾可以包含零个、一个或两个填充字符。
29、这一条不是C#的,是数据库的,有用,收藏一下~
几个删除重复记录的sql语句
(1)用rowid方法据据oracle带的rowid属性,进行判断,是否存在重复,语句如下:
查数据:
select * from table1 a where rowid !=(select max(rowid)
from table1 b where a.name1=b.name1 and a.name2=b.name2......)
删数据:
delete from table1 a where rowid !=(select max(rowid)
from table1 b where a.name1=b.name1 and a.name2=b.name2......)
(2)group by方法
查数据:
select count(num), max(name) from student --列出重复的记录数,并列出他的name属性
group by num
having count(num) >1 --按num分组后找出表中num列重复,即出现次数大于一次
删数据:
delete from student
group by num
having count(num) >1
这样的话就把所有重复的都删除了。
(3)用distinct方法 -对于小的表比较有用
create table table_new as select distinct * from table1 minux
truncate table table1;
insert into table1 select * from table_new;