WebForm开发常用代码
1.获取服务器绝对路径:
public static string GetMapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', )).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
2.简单实例序列化:实体类增加特性:[Serializable],实用:
序列化:
public static void Save(object obj, string filename)
{
FileStream fs = null;
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
反序列化:
public static object Load(Type type, string filename)
{
FileStream fs = null;
try
{
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
3、URL字符编码:HttpContext.Current.Server.UrlEncode(str)、HttpContext.Current.Server.UrlDecode(str)
获取web.config中appSettings字节值:ConfigurationManager.AppSettings[xmlName].ToString()
4、int.TryParse与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。
最后一个参数为输出值,如果转换失败,输出值为 0,如果转换成功,输出值为转换后的int值
public GetInt(string str, int defaultValue)
{
int result = 0;
int.TryParse(str, out result);
return result == 0 ? defaultValue : result;
}
5.对得到的值要有是否为空的判断
6、禁止回车表单自动提交:onkeydown="if(event.keyCode==13){return false;}"
7、IO相关:
1)获取文件夹下文件: string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
2)获取文件名:Path.GetFileNameWithoutExtension(file);
3)读取:string[] sqlItems = File.ReadAllLines(path); string text=FileExtend.ReadAllText(text);
Byte[] buff = File.ReadAllBytes(path); Encoding.Default.GetString(buff);
8、Request对象: [URL]:LocalPath:映射路径,[UrlReferrer]:PathAndQuery
Response:
9、Reflection:
Assembly ass = Assembly.Load(DllName);//完整DLL
Type[] typeList = ass.GetExportedTypes();
Type:Name,FullName,BaseType
MethodInfo:Name,GetCustomAttributes
GetProperty().GetValue
实例化:object o = Activator.CreateInstance(t); t.GetMethod("ProcessRequest").Invoke(o, new object[] { });
10、WriteCookie
HttpCookie tokenCookie = new HttpCookie("name", value);
tokenCookie.Domain = ;
tokenCookie.Expires = DateTime.Now.AddHours();
HttpContext.Current.Response.Cookies.Add(tokenCookie);
11、int补齐字符串:string.padledt(len,'0'); int.tostring("d8")
WebForm开发常用代码的更多相关文章
- 36个Android开发常用代码片段
//36个Android开发常用代码片段 //拨打电话 public static void call(Context context, String phoneNumber) { context.s ...
- 23个phpcms v9模板制作及二次开发常用代码案例
0:调用最新文章,带所在版块 {pc:get sql="SELECT a.title, a.catid, b.catid, b.catname, a.url as turl ,b.url a ...
- ASP.NET MVC+EF5 开发常用代码
Asp.Net Mvc,EF 技术常用点总结 1.Asp.Net MVC a)获得当前控制器名和当前操作的名称(action) 1.Action 中 RouteData.Values[" ...
- 56个PHP开发常用代码
2016/02/14 6203 4 在编写代码的时候有个神奇的工具总是好的!下面这里收集了 50+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常 ...
- iOS开发常用代码块(第二弹)
GCD定时器 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); dispat ...
- iOS开发常用代码块
遍历可变数组的同时删除数组元素 NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array]; NSString *str1 = ...
- iOS开发常用代码块(2)
GCD定时器 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispa ...
- Android开发常用代码片段
拨打电话 public static void call(Context context, String phoneNumber) { context.startActivity( new Inten ...
- Java开发常用代码
1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric string int i = Integer.parseInt(a) ...
随机推荐
- ASP 验证、查询AD域账户信息
'''函数功能:查询域用户信息 '''参数说明:strAdmin-域管理账户:Password-域帐户密码:Domain-域服务器. ''' ''' 参考资料:http://www.experts-e ...
- windows下端口被占用的解决方法
1:打开CMD输入:netstat -ano | findstr "80" 找到PID: 2:查看应用名称:tasklist | findstr "2544" ...
- 在 ActionBar 添加刷新按钮
在以前版本的 Gmail 应用中,ActionBar 上有个刷新菜单,点击一下刷新菜单变成一个转圈的刷新标示动画图片. 之前实现该功能的时候都是使用一个类库 RefreshActionItem来实现的 ...
- tomcat 系统架构与设计模式 第二部分 设计模式 转
Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析 许 令波, Java 开发工程师, 淘宝网 许令波,现就职于淘宝网,是一名 Java 开发工程师.对大型互联网架构设计颇感兴趣,并对一些 ...
- WordPress wp-includes/functions.php脚本远程任意代码执行漏洞
漏洞名称: WordPress wp-includes/functions.php脚本远程任意代码执行漏洞 CNNVD编号: CNNVD-201309-166 发布时间: 2013-09-13 更新时 ...
- Android TextView setText内嵌html标签
由于得到的数据是保存在数据库里面的,不好对数据的某一部分进行操作.解决办法在数据库里面存数据的时候加上html的标签对, 如data = <中华人名共和国道路交通安全实施条例>第<u ...
- 网络流CodeForces. Original 589F:Gourmet and Banquet
A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...
- org.springframework.web.context.ContextLoaderListener 转
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web ...
- Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
- PHP 小代码
//获取网上的一个文件function getUrlImage($url, $file = '', $maxExe = 0, $safe = false){ $urlExt = explode('.' ...