zoukankan
html css js c++ java
常见的类整理
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.OleDb;
using
System.Data.SqlClient;
using
System.Drawing;
using
System.Text.RegularExpressions;
//
regex
using
System.Text;
//
Encoding
using
System.Security;
//
登录验证
using
System.IO;
/**/
///
<summary>
///
Summary description for config
///
</summary>
public
class
config
{
构造函数
#region
构造函数
public
config()
{
//
//
TODO: Add constructor logic here
//
}
#endregion
access建立连接
#region
access建立连接
public
static
OleDbConnection createCon()
{
OleDbConnection con
=
new
OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings[
"
creTableConnectionString
"
].ConnectionString);
return
con;
}
#endregion
sqlserver2000建立连接
#region
sqlserver2000建立连接
public
static
SqlConnection createSqlCon()
{
SqlConnection consql
=
new
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[
"
creTableConnectionString
"
].ConnectionString);
return
consql;
}
#endregion
过滤单引号
#region
过滤单引号
/**/
///
<summary>
///
过滤单引号 返回String
///
</summary>
///
<param name="Str"></param>
///
<returns></returns>
public
static
string
Chk39(
string
Str)
{
if
((Str
==
""
)
||
(Str
==
null
))
Str
=
""
;
else
Str
=
Str.Replace(
"
'
"
,
"
'
"
);
return
Str;
}
#endregion
反过滤单引号
#region
反过滤单引号
/**/
///
<summary>
///
反过滤单引号 Return String
///
</summary>
///
<param name="Str"></param>
///
<returns></returns>
public
static
string
ReChk39(
string
Str)
{
Str
=
Str.Replace(
"
'
"
,
"
'
"
);
return
Str;
}
#endregion
判断用户是否登录
#region
判断用户是否登录
public
static
void
sessionIS()
//
判断用户是否登陆
{
//
System.Web.HttpResponse response = new System.Web.HttpContext.
//
System.Web.HttpResponse response = new System.Web.HttpContext.
System.Web.HttpResponse response
=
System.Web.HttpContext.Current.Response;
string
sid
=
Convert.ToString(System.Web.HttpContext.Current.Session[
"
USERID
"
]);
string
name
=
Convert.ToString(System.Web.HttpContext.Current.Session[
"
USERNAME
"
]);
if
(name
==
null
||
name
==
""
||
sid
==
null
||
sid
==
""
)
{
response.Write(
"
<script>window.parent.location.href='login.aspx';</script>
"
);
response.End();
}
}
#endregion
弹出JavaScript小窗口
#region
弹出JavaScript小窗口
/**/
///
<summary>
///
弹出JavaScript小窗口
///
</summary>
///
<param name="js">
窗口信息
</param>
public
static
void
Alert(
string
message)
{
string
js
=
@"
<Script language='JavaScript'>
alert('
"
+
message
+
"
');</Script>
"
;
HttpContext.Current.Response.Write(js);
}
#endregion
弹出消息框并且转向到新的
#region
弹出消息框并且转向到新的
/**/
///
/
<summary>
///
弹出消息框并且转向到新的URL
///
</summary>
///
<param name="message">
消息内容
</param>
///
<param name="toURL">
连接地址
</param>
public
static
void
AlertAndRedirect(
string
message,
string
toURL)
{
string
js
=
"
<script language=javascript>alert('{0}');window.location.replace('{1}')</script>
"
;
HttpContext.Current.Response.Write(
string
.Format(js, message, toURL));
}
#endregion
回到历史页面
#region
回到历史页面
/**/
/**/
/**/
///
<summary>
///
回到历史页面
///
</summary>
///
<param name="value">
-1/1
</param>
public
static
void
GoHistory(
int
value)
{
string
js
=
@"
<Script language='JavaScript'>
history.go({0});
</Script>
"
;
HttpContext.Current.Response.Write(
string
.Format(js, value));
}
#endregion
关闭当前窗口
#region
关闭当前窗口
/**/
/**/
/**/
///
<summary>
///
关闭当前窗口
///
</summary>
public
static
void
CloseWindow()
{
string
js
=
@"
<Script language='JavaScript'>
parent.opener=null;window.close();
</Script>
"
;
HttpContext.Current.Response.Write(js);
HttpContext.Current.Response.End();
}
#endregion
刷新父窗口
#region
刷新父窗口
/**/
/**/
/**/
///
<summary>
///
刷新父窗口
///
</summary>
public
static
void
RefreshParent(
string
url)
{
string
js
=
@"
<Script language='JavaScript'>
window.opener.location.href='
"
+
url
+
"
';window.close();</Script>
"
;
HttpContext.Current.Response.Write(js);
}
#endregion
刷新打开窗口
#region
刷新打开窗口
/**/
/**/
/**/
///
<summary>
///
刷新打开窗口
///
</summary>
public
static
void
RefreshOpener()
{
string
js
=
@"
<Script language='JavaScript'>
windows.opener.location.reload();
</Script>
"
;
HttpContext.Current.Response.Write(js);
}
#endregion
打开指定大小的新窗体
#region
打开指定大小的新窗体
/**/
/**/
/**/
///
<summary>
///
打开指定大小的新窗体
///
</summary>
///
<param name="url">
地址
</param>
///
<param name="width">
宽
</param>
///
<param name="heigth">
高
</param>
///
<param name="top">
头位置
</param>
///
<param name="left">
左位置
</param>
public
static
void
OpenWebFormSize(
string
url,
int
width,
int
heigth,
int
top,
int
left)
{
string
js
=
@"
<Script language='JavaScript'>window.open('
"
+
url
+
@"
','','height=
"
+
heigth
+
"
,width=
"
+
width
+
"
,top=
"
+
top
+
"
,left=
"
+
left
+
"
,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>
"
;
HttpContext.Current.Response.Write(js);
}
#endregion
转向Url制定的页面
#region
转向Url制定的页面
/**/
/**/
/**/
///
<summary>
///
转向Url制定的页面
///
</summary>
///
<param name="url">
连接地址
</param>
public
static
void
JavaScriptLocationHref(
string
url)
{
string
js
=
@"
<Script language='JavaScript'>
window.location.replace('{0}');
</Script>
"
;
js
=
string
.Format(js, url);
HttpContext.Current.Response.Write(js);
}
#endregion
打开指定大小位置的模式对话框
#region
打开指定大小位置的模式对话框
/**/
/**/
/**/
///
<summary>
///
打开指定大小位置的模式对话框
///
</summary>
///
<param name="webFormUrl">
连接地址
</param>
///
<param name="width">
宽
</param>
///
<param name="height">
高
</param>
///
<param name="top">
距离上位置
</param>
///
<param name="left">
距离左位置
</param>
public
static
void
ShowModalDialogWindow(
string
webFormUrl,
int
width,
int
height,
int
top,
int
left)
{
string
features
=
"
dialogWidth:
"
+
width.ToString()
+
"
px
"
+
"
;dialogHeight:
"
+
height.ToString()
+
"
px
"
+
"
;dialogLeft:
"
+
left.ToString()
+
"
px
"
+
"
;dialogTop:
"
+
top.ToString()
+
"
px
"
+
"
;center:yes;help=no;resizable:no;status:no;scroll=yes
"
;
ShowModalDialogWindow(webFormUrl, features);
}
public
static
void
ShowModalDialogWindow(
string
webFormUrl,
string
features)
{
string
js
=
ShowModalDialogJavascript(webFormUrl, features);
HttpContext.Current.Response.Write(js);
}
public
static
string
ShowModalDialogJavascript(
string
webFormUrl,
string
features)
{
string
js
=
@"
<script language=javascript>
showModalDialog('
"
+
webFormUrl
+
"
','','
"
+
features
+
"
');</script>
"
;
return
js;
}
#endregion
过滤输入的信息
#region
过滤输入的信息
/**/
///
<summary>
///
过滤输入的信息
///
</summary>
///
<param name="text">
要过滤的字符串
</param>
///
<param name="maxLength">
最大长度
</param>
///
<returns></returns>
public
static
string
InputText(
string
text,
int
maxLength)
{
text
=
text.Trim();
if
(
string
.IsNullOrEmpty(text))
return
string
.Empty;
if
(text.Length
>
maxLength)
text
=
text.Substring(
0
, maxLength);
text
=
Regex.Replace(text,
"
[\\s]{2,}
"
,
"
"
);
//
two or more spaces
text
=
Regex.Replace(text,
"
(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)
"
,
"
\n
"
);
//
<br>
text
=
Regex.Replace(text,
"
(\\s*&[n|N][b|B][s|S][p|P];\\s*)+
"
,
"
"
);
//
text
=
Regex.Replace(text,
"
<(.|\\n)*?>
"
,
string
.Empty);
//
any other tags
text
=
text.Replace(
"
'
"
,
"
''
"
);
return
text;
}
#endregion
截取字符串函数
#region
截取字符串函数
/**/
///
<summary>
///
截取字符串函数
///
</summary>
///
<param name="str">
所要截取的字符串
</param>
///
<param name="num">
截取字符串的长度
</param>
///
<returns></returns>
public
static
string
GetSubString(
string
str,
int
num)
{
return
(str.Length
>
num)
?
str.Substring(
0
, num)
+
""
: str;
}
#endregion
特殊字符的转换
#region
特殊字符的转换
public
static
string
Encode(
string
str)
{
str
=
str.Replace(
"
'
"
,
"
'
"
);
str
=
str.Replace(
"
\
""
,
"
&
quot;
"
);
str
=
str.Replace(
"
<
"
,
"
<
"
);
str
=
str.Replace(
"
>
"
,
"
>
"
);
return
str;
}
public
static
string
Decode(
string
str)
{
str
=
str.Replace(
"
>
"
,
"
>
"
);
str
=
str.Replace(
"
<
"
,
"
<
"
);
str
=
str.Replace(
"
"
,
"
"
);
str
=
str.Replace(
"
"
"
,
"
\
""
);
return
str;
}
#endregion
字符传的转换 用在查询 登陆时 防止恶意的盗取密码
#region
字符传的转换 用在查询 登陆时 防止恶意的盗取密码
public
static
string
TBCode(
string
strtb)
{
strtb
=
strtb.Replace(
"
!
"
,
""
);
strtb
=
strtb.Replace(
"
@
"
,
""
);
strtb
=
strtb.Replace(
"
#
"
,
""
);
strtb
=
strtb.Replace(
"
$
"
,
""
);
strtb
=
strtb.Replace(
"
%
"
,
""
);
strtb
=
strtb.Replace(
"
^
"
,
""
);
strtb
=
strtb.Replace(
"
&
"
,
""
);
strtb
=
strtb.Replace(
"
*
"
,
""
);
strtb
=
strtb.Replace(
"
(
"
,
""
);
strtb
=
strtb.Replace(
"
)
"
,
""
);
strtb
=
strtb.Replace(
"
_
"
,
""
);
strtb
=
strtb.Replace(
"
+
"
,
""
);
strtb
=
strtb.Replace(
"
|
"
,
""
);
strtb
=
strtb.Replace(
"
?
"
,
""
);
strtb
=
strtb.Replace(
"
/
"
,
""
);
strtb
=
strtb.Replace(
"
.
"
,
""
);
strtb
=
strtb.Replace(
"
>
"
,
""
);
strtb
=
strtb.Replace(
"
<
"
,
""
);
strtb
=
strtb.Replace(
"
{
"
,
""
);
strtb
=
strtb.Replace(
"
}
"
,
""
);
strtb
=
strtb.Replace(
"
[
"
,
""
);
strtb
=
strtb.Replace(
"
]
"
,
""
);
strtb
=
strtb.Replace(
"
-
"
,
""
);
strtb
=
strtb.Replace(
"
=
"
,
""
);
strtb
=
strtb.Replace(
"
,
"
,
""
);
return
strtb;
}
#endregion
获取汉字第一个拼音
#region
获取汉字第一个拼音
/**/
/**/
/**/
///
<summary>
///
获取汉字第一个拼音
///
</summary>
///
<param name="input"></param>
///
<returns></returns>
static
public
string
getSpells(
string
input)
{
int
len
=
input.Length;
string
reVal
=
""
;
for
(
int
i
=
0
; i
<
len; i
++
)
{
reVal
+=
getSpell(input.Substring(i,
1
));
}
return
reVal;
}
static
public
string
getSpell(
string
cn)
{
byte
[] arrCN
=
Encoding.Default.GetBytes(cn);
if
(arrCN.Length
>
1
)
{
int
area
=
(
short
)arrCN[
0
];
int
pos
=
(
short
)arrCN[
1
];
int
code
=
(area
<<
8
)
+
pos;
int
[] areacode
=
{
45217
,
45253
,
45761
,
46318
,
46826
,
47010
,
47297
,
47614
,
48119
,
48119
,
49062
,
49324
,
49896
,
50371
,
50614
,
50622
,
50906
,
51387
,
51446
,
52218
,
52698
,
52698
,
52698
,
52980
,
53689
,
54481
}
;
for
(
int
i
=
0
; i
<
26
; i
++
)
{
int
max
=
55290
;
if
(i
!=
25
) max
=
areacode[i
+
1
];
if
(areacode[i]
<=
code
&&
code
<
max)
{
return
Encoding.Default.GetString(
new
byte
[]
{ (
byte
)(
65
+
i) }
);
}
}
return
"
?
"
;
}
else
return
cn;
}
#endregion
半角转全角
#region
半角转全角
/**/
/**/
/**/
///
<summary>
///
半角转全角
///
</summary>
///
<param name="BJstr"></param>
///
<returns></returns>
static
public
string
GetQuanJiao(
string
BJstr)
{
char
[] c
=
BJstr.ToCharArray();
for
(
int
i
=
0
; i
<
c.Length; i
++
)
{
byte
[] b
=
System.Text.Encoding.Unicode.GetBytes(c, i,
1
);
if
(b.Length
==
2
)
{
if
(b[
1
]
==
0
)
{
b[
0
]
=
(
byte
)(b[
0
]
-
32
);
b[
1
]
=
255
;
c[i]
=
System.Text.Encoding.Unicode.GetChars(b)[
0
];
}
}
}
string
strNew
=
new
string
(c);
return
strNew;
}
#endregion
全角转半角
#region
全角转半角
/**/
/**/
/**/
///
<summary>
///
全角转半角
///
</summary>
///
<param name="QJstr"></param>
///
<returns></returns>
static
public
string
GetBanJiao(
string
QJstr)
{
char
[] c
=
QJstr.ToCharArray();
for
(
int
i
=
0
; i
<
c.Length; i
++
)
{
byte
[] b
=
System.Text.Encoding.Unicode.GetBytes(c, i,
1
);
if
(b.Length
==
2
)
{
if
(b[
1
]
==
255
)
{
b[
0
]
=
(
byte
)(b[
0
]
+
32
);
b[
1
]
=
0
;
c[i]
=
System.Text.Encoding.Unicode.GetChars(b)[
0
];
}
}
}
string
strNew
=
new
string
(c);
return
strNew;
}
#endregion
生成随机数
#region
生成随机数
/**/
/**/
/**/
///
<summary>
///
生成随机数
///
</summary>
///
<returns></returns>
static
private
string
GenerateCheckCode()
{
int
number;
char
code;
string
checkCode
=
String.Empty;
System.Random random
=
new
Random();
for
(
int
i
=
0
; i
<
5
; i
++
)
{
number
=
random.Next();
if
(number
%
2
==
0
)
code
=
(
char
)(
'
0
'
+
(
char
)(number
%
10
));
else
code
=
(
char
)(
'
A
'
+
(
char
)(number
%
26
));
checkCode
+=
code.ToString();
}
System.Web.HttpContext.Current.Response.Cookies.Add(
new
HttpCookie(
"
CheckCode
"
, checkCode));
return
checkCode;
}
#endregion
生成验证码图片生成验证码图片
#region
生成验证码图片生成验证码图片
/**/
///
<summary>
///
生成验证码图片
///
</summary>
public
static
void
CreateCheckCodeImage()
{
string
checkCode
=
GenerateCheckCode();
System.Web.HttpContext.Current.Session[
"
username
"
]
=
checkCode ;
//
Console.Write(checkCode);
if
(checkCode
==
null
||
checkCode.Trim()
==
String.Empty)
return
;
System.Drawing.Bitmap image
=
new
System.Drawing.Bitmap((
int
)Math.Ceiling((checkCode.Length
*
12.5
)),
22
);
Graphics g
=
Graphics.FromImage(image);
try
{
//
生成随机生成器
Random random
=
new
Random();
//
清空图片背景色
g.Clear(Color.White);
//
画图片的背景噪音线
for
(
int
i
=
0
; i
<
25
; i
++
)
{
int
x1
=
random.Next(image.Width);
int
x2
=
random.Next(image.Width);
int
y1
=
random.Next(image.Height);
int
y2
=
random.Next(image.Height);
g.DrawLine(
new
Pen(Color.Silver), x1, y1, x2, y2);
}
Font font
=
new
System.Drawing.Font(
"
Arial
"
,
12
, (System.Drawing.FontStyle.Bold
|
System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush
=
new
System.Drawing.Drawing2D.LinearGradientBrush(
new
Rectangle(
0
,
0
, image.Width, image.Height), Color.Blue, Color.DarkRed,
1.2f
,
true
);
g.DrawString(checkCode, font, brush,
2
,
2
);
//
画图片的前景噪音点
for
(
int
i
=
0
; i
<
100
; i
++
)
{
int
x
=
random.Next(image.Width);
int
y
=
random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//
画图片的边框线
g.DrawRectangle(
new
Pen(Color.Silver),
0
,
0
, image.Width
-
1
, image.Height
-
1
);
System.IO.MemoryStream ms
=
new
System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType
=
"
image/Gif
"
;
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
#endregion
获取文本框中输入内容
#region
获取文本框中输入内容
/**/
/**/
/**/
///
<summary>
///
获取文本框中输入内容
///
</summary>
///
<param name="ctrl">
TextBox控件ID
</param>
///
<returns></returns>
static
public
string
GetText(TextBox ctrl)
{
return
ctrl.Text.Trim().Replace(
"
'
"
,
"
\
""
);
}
#endregion
清除文本框内容
#region
清除文本框内容
/**/
/**/
/**/
///
<summary>
///
清除文本框内容
///
</summary>
///
<param name="list">
TextBox控件数组
</param>
static
public
void
ClearText(TextBox[] list)
{
foreach
(TextBox ctrl
in
list)
ctrl.Text
=
null
;
}
#endregion
加密的类型
#region
加密的类型
/**/
/**/
/**/
///
<summary>
///
加密的类型
///
</summary>
public
enum
PasswordType
{
SHA1,
MD5
}
#endregion
字符串加密
#region
字符串加密
/**/
///
<summary>
///
字符串加密
///
</summary>
///
<param name="PasswordString">
要加密的字符串
</param>
///
<param name="PasswordFormat">
要加密的类别
</param>
///
<returns></returns>
static
public
string
EncryptPassword(
string
PasswordString,
string
PasswordFormat)
{
switch
(PasswordFormat)
{
case
"
SHA1
"
:
{
PasswordString
=
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString,
"
SHA1
"
);
break
;
}
case
"
MD5
"
:
{
PasswordString
=
System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString,
"
MD5
"
);
break
;
}
default
:
{
//
passWord = string.Empty;
break
;
}
}
return
PasswordString;
}
#endregion
}
查看全文
相关阅读:
Visual Studio 2005 不能调试的问题
自学C语言_第一章
批处理For循环一键Update补丁程序
小米MiFlash刷机报错售后方法参考
Civil 3D 2012 CAD安装完成后打开报错“致命错误:Unhandled Delayload "D3DCOMPILER_47.dll"
Windows查看端口被占用
vc ++6.0打开或者添加出现错误解决方案
一天总结
一天总结
一天总结
原文地址:https://www.cnblogs.com/Hdsome/p/1215216.html
最新文章
C#得到CPU的序列号、硬盘序列号、网卡序列号
[设计思想]单实例设计模式的实现
[读书笔记]鸟哥的LINUX私房菜 十三章 shell scripts
[读书笔记]编程之美
[算法]求最长公共子序列
[读书笔记]第二次看C++ primer
[读书笔记]软件测试 Ron Patton
[转]simhash进行文本查重
[算法]查找算法
[读书笔记]软件测试的艺术 Glenford Myers
热门文章
[读书笔记]鸟哥的LINUX私房菜 网络基础篇
某壳对.Net程序加密的原理及解密探讨四(翻译IL字节码)
IL字节码解码工具更新 V2.0
某壳对.Net程序加密的原理及解密探讨二
DataGridView 绑定DataTable方式编辑保存的bug?
IL字节码解码工具更新 V1.85
某壳对.Net程序加密的原理及解密探讨一
某壳对.Net程序加密的原理及解密探讨五(元数据还原以及IL解码的改进)
在 C# 中动态调用 native dll 的导出函数
某壳对.Net程序加密的原理及解密探讨三(实例解密)
Copyright © 2011-2022 走看看