web.config and split 用法
一:通常我们把数据库的连接语句放在web.config中,因为这样便于修改,如果让在类库中,这样生成了dll就不方便修改了。
比如在web.config中增加数据库连接语句:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;User ID=sa;Password=sa;Initial Catalog=votetest;"/>
</connectionStrings>
在类先要导入 using System.Configuration;(一般情况下增加类的时候自动导入)
然后ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString,ok了
ConfigurationManager是表示web.config类对象 然后引用ConnectionStrings。。。
二:split用法:split表示分离字符串;
如:string str="hello| world"
string strSplite=str.split('|')
则 strSplit[0]="hello";
strSoplit[1]="world"
下面以一个在线编辑的图片上传利用web.config修改图片的上传的文件夹以及大小
先看web。config:
<appSettings>
<add key="Insert_picture_editor_parameters" value="upload|204800" />
</appSettings>
然后看:c#源文件:
void upfile()
{
#region
Random myrd = new Random();
string path, filename, upfilepath; //上传文件路径,上传文件名称
string[] temps =ConfigurationSettings.AppSettings["Insert_picture_editor_parameters"].ToString().Split('|');
path = Server.MapPath("~/"+temps[0]+"/");
filename = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + myrd.Next(1000).ToString(); //上传文件名称
//取得文件的扩展名
string fileExtension = System.IO.Path.GetExtension(File1.PostedFile.FileName).ToLower();
path = path + filename + fileExtension;
if (fileExtension != ".jpg" && fileExtension != ".gif" && fileExtension != ".jpeg" && fileExtension != ".png")
{
string js = @"<Script language='JavaScript'>alert('错误!!文件类型必须为jpg或者gif或者png!');</Script>";
HttpContext.Current.Response.Write(js);
}
else if (File1.PostedFile.ContentLength > int.Parse(temps[1]))
{
string js = @"<Script language='JavaScript'>alert('错误!!文件大小不能超过" + temps[1] + "字节!');</Script>";(temps[1]就是204800)
HttpContext.Current.Response.Write(js);
}
else
{
upfilepath = temps[0] + "/" + filename + fileExtension;//前面出掉了 "/" + (temp[0]就是split分离的第一个字符串也就是upload)
File1.PostedFile.SaveAs(path);
string js = @"<script language=javascript>";
js += " window.returnValue = '<img src= \"" + upfilepath + "\"/>';";
js += "window.close();";
js += "</script>";
Response.Write(js);
}