一分耕耘,一分收获

编程路上的成长与收获

 

2007年12月13日

C#文件操作

C#追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");
sw.WriteLine("追逐理想");
sw.WriteLine("kzlll");
sw.WriteLine(".NET笔记");
sw.Flush();
sw.Close();
C#拷贝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Copy(OrignFile,NewFile,true);

C#删除文件
string delFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Delete(delFile);

C#移动文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"\\myText.txt";
NewFile = Server.MapPath(".")+"\\myTextCopy.txt";
File.Move(OrignFile,NewFile);

C#创建目录
// 创建目录c:\sixAge
DirectoryInfo d=Directory.CreateDirectory("c:\\sixAge");
// d1指向c:\sixAge\sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2指向c:\sixAge\sixAge1\sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
// 将当前目录设为c:\sixAge
Directory.SetCurrentDirectory("c:\\sixAge");
// 创建目录c:\sixAge\sixAge2
Directory.CreateDirectory("sixAge2");
// 创建目录c:\sixAge\sixAge2\sixAge2_1
Directory.CreateDirectory("sixAge2\\sixAge2_1");

递归删除文件夹及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>
public void DeleteFolder(string dir)
{
    if (Directory.Exists(dir)) //如果存在这个文件夹删除之
    {
        foreach(string d in Directory.GetFileSystemEntries(dir))
        {
            if(File.Exists(d))
                File.Delete(d); //直接删除其中的文件
            else
                DeleteFolder(d); //递归删除子文件夹
        }
        Directory.Delete(dir); //删除已空文件夹
        Response.Write(dir+" 文件夹删除成功");
    }
    else
        Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示
}

protected void Page_Load (Object sender ,EventArgs e)
{
    string Dir="D:\\gbook\\11";
    DeleteFolder(Dir); //调用函数删除文件夹
}


// ======================================================
  // 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面
  // 如果目标文件夹为只读属性就会报错。
  // April 18April2005 In STU
  // ======================================================
  public static void CopyDir(string srcPath,string aimPath)
  {
   try
   {
    // 检查目标目录是否以目录分割字符结束如果不是则添加之
    if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
     aimPath += Path.DirectorySeparatorChar;
    // 判断目标目录是否存在如果不存在则新建之
    if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
    // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
    // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
    // string[] fileList = Directory.GetFiles(srcPath);
    string[] fileList = Directory.GetFileSystemEntries(srcPath);
    // 遍历所有的文件和目录
    foreach(string file in fileList)
    {
     // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
     if(Directory.Exists(file))
      CopyDir(file,aimPath+Path.GetFileName(file));
      // 否则直接Copy文件
     else
      File.Copy(file,aimPath+Path.GetFileName(file),true);
    }
   }
   catch (Exception e)
   {
    MessageBox.Show (e.ToString());
   }
  }

  // ======================================================
  // 实现一个静态方法将指定文件夹下面的所有内容Detele
  // 测试的时候要小心操作,删除之后无法恢复。
  // April 18April2005 In STU
  // ======================================================
  public static void DeleteDir(string aimPath)
  {
   try
   {
    // 检查目标目录是否以目录分割字符结束如果不是则添加之
    if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
     aimPath += Path.DirectorySeparatorChar;
    // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
    // 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
    // string[] fileList = Directory.GetFiles(aimPath);
    string[] fileList = Directory.GetFileSystemEntries(aimPath);
    // 遍历所有的文件和目录
    foreach(string file in fileList)
    {
     // 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
     if(Directory.Exists(file))
     {
      DeleteDir(aimPath+Path.GetFileName(file));
     }
      // 否则直接Delete文件
     else
     {
      File.Delete (aimPath+Path.GetFileName(file));
     }
    }
    //删除文件夹
    System.IO .Directory .Delete (aimPath,true);
   }
   catch (Exception e)
   {
    MessageBox.Show (e.ToString());
   }
  }

 

posted @ 2007-12-13 15:03 Keren 阅读(69) | 评论 (0)编辑

2007年12月5日

谈2.0网站初期规划应该考虑的问题[转载]



我做过多个2.0公司的技术顾问,简单的谈谈2.0公司遇到的问题(涉及隐私,我用A B C D代替),这里就不再赘述大家众所周知的页面静态化,缓存和代码安全等问题了,有点技术的2.0公司的CTO都知道这些东西,我们谈点发展之后的问题

A公司

A公司做的是SNS网站,程序是两个毛头小伙子做的,目标直指51,程序开发是一帆风顺,功能也比51牛多了,推广也是一帆风顺(A公司有自己独到的推广方式。但是当ALEXA到2W的时候问题出来了,每天下午4点左右,网站速度慢的惊人,基本上打不开,公司三台服务器CPU100%,让人郁闷的是公司的网络配置方式,居然是双WEB的集群,而单独一台DB数据库。整个瓶颈在数据库,于是我建议做DB的集群,分析了一下数据结构,MD,典型的WEB程序员的作品,没有一点数据库设计规范,功能实现是可以,如果要扩展,不可能,集群基本上是不可能的,怎么办?不能办,于是,一个月的时间修改程序,数据结构基本上换了一遍 前期砸进去的几十万打了水飘,用户走光了。

结论:WEB2.0前期设计的时候不应该只考虑功能,应该认真考虑一下底层和数据结构了。

B公司

B公司也是做的SNS网站,程序是3个人开发的,CEO是某名牌大学的经济学硕士,有点知己网的味道,又有一些特色出来,说实话,公司的潜力不错,CEO 有很强的运作能力,感觉前景不错。系统架构还行,但是---但是系统崩溃了,why?系统没有考虑到用户有个海量的说法,文件也有个海量的说法,用户的相册,图片全部存贮在WEB服务器的一个分区上,每个用户一个目录,而打开性能监视器,磁盘的IO高的惊人,基本上无暇响应。众所周知,文件系统也是一个数据库,单独大文件无所谓,关键是整个是300多个G的零碎文件,大量的读写操作,系统崩溃,数据丢失,文件系统的一个链断了,用户数据全部丢失!!!这是一个非常沉重的问题,系统整整停了一个月来做数据恢复(单独文件很容易,但是海量文件目前还没有一个软件能组织起来软件架构)。解决方案:修改程序架构,做分布式文件存贮(程序修改用了8天,但是文件转移却又用去了将近一个月),20万用户损失殆尽

结论:WEB2.0前期的设计应该有应付海量存贮的考虑,整个涉及了程序架构的修改,前期规划不好的话基本上思路一条。

C公司

C公司是一个值得尊敬的公司,CEO技术出身,和比尔盖茨一样,大学未毕业出来做网络,01到03年做短信狠赚了一笔,后来做的小项目也小有所成,说实话,我很佩服。公司做的是校友方面,但是更偏重myspace风格,注重个人主页,推广方面也下了大手笔。系统崩溃的原因其实很简单,由于采用的是微软的 SqlServer,而微软直接就告诉了我们,SQLSERVER不支持集群,他们的数据库超负载,100%就没有下去过,只能横向增加配置,采用了4路 4核CPU系统,但是系统还是崩溃了... 高互动注定了高负载。解决方案:现从基本入手,解决掉几个程序耗能大户,对数据库采用横向切割,将用户每10万进行分组,同时对数据库系统进行散列,将多个表垂直分割,同时进行文件分组,解决问题. 因为修改了数据结构,程序也基本上大动了一下。 好在系统没有出大错,损失不算很大,不过对用户体验造成了很坏的影响。

结论:WEB2.0前期设计应该有良好的散列考虑,程序应该能有配合的扩充性,符合数据库的扩充

D公司

D公司是一个各个方面做的比较好的公司,做了CDN加速,图片也独立分出了N个服务器,数据库不错的一个,(CTO是个数据库专家),系统崩溃的原因在于 WEB,按道理说WEB很容易做集群的,但是发现集群并解决不掉问题,他们的集群只允许做4台的WEB集群,但是4台都当掉了。仔细分析,找到原因,我估计整个也是大部分CTO最容易犯的一个错误,或者说他们根本就想不到的问题,就是WEB上传的问题,上传的时候由于时间的原因,线程是保持链接的,300 个线程就可以把一个WEB Server当掉了。解决方案:这个最简单,把上传和其他耗能大户分离出独立出来。程序改动不是很大,但是之前半个月速度满对用户体验的损失也不可小视。

结论:没有什么结论了,毕竟有海量访问经验的CTO不多,也就是那几个大站的。

总结:不是泼冷水,模仿其实是很容易的,随便找几个WEB程序员就能做到,并且很简单,速度可能还很高效,因为WEB2.0无非就是跟数据库打交道,会操作数据库就会做。但是真正做大并不容易,因为能应付海量访问的程序并不简单,现在的程序员都太自命不凡,其实真正有经验的并不多,不要相信一个月薪5K- -10K的程序员能给你多大的惊喜,能应付海量访问的程序员不是那个价格。如果您想做2.0,想做大,有几个个建议:

一.找DBMS的专家设计好数据库,大部分程序员都不知道分区视图,数据散列,数据组的概念

二.设计好程序架构(这个其实不难,有个高人指导就行了),保持良好的扩展性,成本考虑可以找兼职的系统架构设计师做好系统架构,确定将来的发展瓶颈。

三.考虑好文件存贮的问题。文件存贮的技术含量看起来很低,其实是很高的,可以考虑反向代理的方案。文件存贮出问题了,站点基本上就完蛋了,不仅仅是RAID的问题和存贮服务器的问题,不过道理倒是一点就破的

四.中国国情考虑,这个最致命,需要考虑电信和网通的问题,CDN并不能解决所有问题。互动性的东西并CDN并不是很有效。最关键的是,现有的双线机房遇到DDOS攻击基本上都会当掉,原因很简单,双线机房都是私人机房,本身就不会有太高的带宽,随便攻击一下就可以D掉(顺带提一个笑话,我知道一个双线机房的老总总共1G的带宽却买了4G的金盾墙,很简单800M的攻击就可以搞定)。

五.网络延迟的问题,这是分布式系统必须要考虑的,程序要能容忍0到100秒的数据延迟的功能,也就是同步的问题。不要小看这几十秒,问题很大的,如果你的站点有交互式功能,比如即时聊天,你可以想象一下是个什么结果。对于即时聊天的东西,可以用反向代理来解决(成本较高)。但是对于留言和评论的影响不大,但是如果系统为了健壮做了缓存和静态化的时候,这个东西可能就是灾难性的了。

六.分散你的程序,如果你没有太多的资金构筑动辄百万的服务器,建议把功能分散开来,比如相册一台服务器,留言一台服务器

七.看好你的程序员,如果没有很好的激励措施的话你的程序员很容易写出敷衍性的代码,而这个可能就是将来的大患,程序架构定下来后要修改可能就要费牛劲了。最好你的CTO能对你100%的衷心,100%的负责。

八.文件同步的问题,这个问题可能你觉得没有必要,如果你看一下网通和电信的TTL就明白了,同步要支持续传,并且不能是持续的,否则你的成本会高出N倍,不要期望能通过你的软件实现,交给你的程序员吧,把上面的话告诉他他就知道怎么做了。

九.最狠的一个问题了,也是吃亏最大的问题,不管您跟网警的关系多好,看好你的用户,审核好你的东西,一被停机可能就致命,本人就吃过N次亏。

十.最后,祝各位站长一番风顺,大展宏图。

虽然我没打算怎么创业,暂时好好搞好技术,但是看了这篇文章,觉得自己好差啊,什么都不知道
所以,准备好好充电下:
1.数据库设计
  数据库设计规范
  分区试图,数据散列,数据组,横向切割,散列,多表的垂直切割,数据库扩充
  集群:数据库集群,WEB集群,sqlserver不支持集群...
2.程序构架
3.文件存储
  海量存储,海量访问,文件分布式存储,文件分组
4.文件同步
  支持续传,文件上传是线程保持链接...,线程,把上传和其他耗能大户分离独立出来
5.网络延迟
6.电信网通
还有,页面应该尽可能静态化,增加缓存,优化索引之类,myspace,网站运作,服务期超载....


还真多呐,太多了,不会的,有前辈给指点下就好了....

posted @ 2007-12-05 11:26 Keren 阅读(59) | 评论 (1)编辑

2007年11月21日

脚本在客户端取label的值

用value取Label的值总是取不到,说是undefined,请教了下其他人,原来是用innerHTML

  valueinnerHTML

取文本框textbox的值用value,label的值用innerHTML


   textbox示例:
   var ddl   = document.getElementById("ddlType");
   var tid = ddl.options[ddl.selectedIndex].value;
   var key = document.getElementById("txtKey").value;
   window.location.href="aa.aspx?Key="+escape(key)+"&tid="+tid+"";

   
   label示例:
   var companycode = document.getElementById("LblcompanyCode").innerHTML;
   window.showModalDialog("aa.aspx?    code="+companycode,"","dialogHeight:300px;dialogWidth:400px;status:off;scroll:off;");
      window.location.href = window.location.href;

posted @ 2007-11-21 11:58 Keren 阅读(206) | 评论 (0)编辑

2007年11月15日

ajax updatepannel中response.write()问题

今天又遇到这个问题,搜了下,整理了看,大概西面三种方法可以解决问题:

protected void Button1_Click(object sender, EventArgs e)
    {
       1. Response.Write("<script language=javascript>\r alert(\"修改失败!\");\r</script>");

       2. ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "alert", "alert('你好 !');", true);      
      3. Page.RegisterStartupScript("alert", "<script language='javascript'>window.showModelessDialog(\"javascript:alert('你好 !');\",\"\",\"status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;\")</script>"); 

       4. //ClientScript.RegisterClientScriptBlock(typeof(UpdatePanel), "alert", "<script>alert('自己看着办!')</script>", true);
    }

前台页面代码;

<form id="form1" runat="server">
    <div>
     <div>
        <asp:ScriptManager id="ScriptManager1" runat="server">
        </asp:ScriptManager>
   
    </div>
        <asp:UpdatePanel id="UpdatePanel1" runat="server">
        <Triggers>
        <asp:PostBackTrigger ControlID="Button1" />
        </Triggers>
            <contenttemplate>
<asp:Button id="Button1" runat="server" Text="Button"  OnClick="Button1_Click"></asp:Button>
</contenttemplate>
        </asp:UpdatePanel>

    </div>
    </form>

其中第一种要加上<Triggers>
        <asp:PostBackTrigger ControlID="Button1" />
        </Triggers>

下面几种不用加。

但是我观察了下,第一种,好像刷新,第二第三种不刷。第四种好像不可以,没有反应...不过网上说可以,不知了...

posted @ 2007-11-15 09:34 Keren 阅读(288) | 评论 (0)编辑

2007年7月27日

MultiView 和 脚本显示

MultiView: 
  
            我在帮助里找了个例子 :
  
<%@ Page Language="C#" %>
<html>
<head>
<script runat="server">
protected void NextButton_Command(object sender, EventArgs e)
{
// Determine which button was clicked
// and set the ActiveViewIndex property to
// the view selected by the user.
if (DevPollMultiView.ActiveViewIndex > -1 & DevPollMultiView.ActiveViewIndex < 3)
{
// Increment the ActiveViewIndex property
// by one to advance to the next view.
DevPollMultiView.ActiveViewIndex += 1;
}
else if (DevPollMultiView.ActiveViewIndex == 3)
{
// This is the final view.
// The user wants to save the survey results.
// Insert code here to save survey results.
// Disable the navigation buttons.
Page4Save.Enabled = false;
Page4Restart.Enabled = false;
}
else
{
throw new Exception("An error occurred.");
}
}
protected void BackButton_Command(object sender, EventArgs e)
{
if (DevPollMultiView.ActiveViewIndex > 0 & DevPollMultiView.ActiveViewIndex <= 2)
{
// Decrement the ActiveViewIndex property
// by one to return to the previous view.
DevPollMultiView.ActiveViewIndex -= 1;
}
else if (DevPollMultiView.ActiveViewIndex == 3)
{
// This is the final view.
// The user wants to restart the survey.
// Return to the first view.
DevPollMultiView.ActiveViewIndex = 0;
}
else
{
throw new Exception("An error occurred.");
}
}
</script>
</head>
<body>
<form ID="Form1" runat="Server">
<h3>MultiView ActiveViewIndex Example</h3>
<asp:Panel id="Page1ViewPanel"
Width="330px"
Height="150px"
HorizontalAlign =Left
Font-size="12"
BackColor="#C0C0FF"
BorderColor="#404040"
BorderStyle="Double"
runat="Server">
<asp:MultiView id="DevPollMultiView"
ActiveViewIndex=0
runat="Server">
<asp:View id="Page1"
runat="Server">
<asp:Label id="Page1Label"
Font-bold="true"
Text="What kind of applications do you develop?"
runat="Server">
</asp:Label><br><br>
<asp:RadioButton id="Page1Radio1"
Text="Web Applications"
Checked="False"
GroupName="RadioGroup1"
runat="server" >
</asp:RadioButton><br>
<asp:RadioButton id="Page1Radio2"
Text="Windows Forms Applications"
Checked="False"
GroupName="RadioGroup1"
runat="server" >
</asp:RadioButton><br><br><br>
<asp:Button id="Page1Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat= "Server">
</asp:Button>
</asp:View>
<asp:View id="Page2"
runat="Server">
<asp:Label id="Page2Label"
Font-bold="true"
Text="How long have you been a developer?"
runat="Server">
</asp:Label><br><br>
<asp:RadioButton id="Page2Radio1"
Text="Less than five years"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br>
<asp:RadioButton id="Page2Radio2"
Text="More than five years"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br><br><br>
<asp:Button id="Page2Back"
Text = "Previous"
OnClick="BackButton_Command"
Height="25"
Width="70"
runat= "Server">
</asp:Button>
<asp:Button id="Page2Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button>
</asp:View>
<asp:View id="Page3"
runat="Server">
<asp:Label id="Page3Label1"
Font-bold="true"
Text= "What is your primary programming language?"
runat="Server">
</asp:Label><br><br>
<asp:RadioButton id="Page3Radio1"
Text="Visual Basic .NET"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br>
<asp:RadioButton id="Page3Radio2"
Text="C#"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br>
<asp:RadioButton id="Page3Radio3"
Text="C++"
Checked="False"
GroupName="RadioGroup1"
runat="Server">
</asp:RadioButton><br><br>
<asp:Button id="Page3Back"
Text = "Previous"
OnClick="BackButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button>
<asp:Button id="Page3Next"
Text = "Next"
OnClick="NextButton_Command"
Height="25"
Width="70"
runat="Server">
</asp:Button><br>
</asp:View>
<asp:View id="Page4"
runat="Server">
<asp:Label id="Label1"
Font-bold="true"
Text = "Thank you for taking the survey."
runat="Server">
</asp:Label>
<br><br><br><br><br><br>
<asp:Button id="Page4Save"
Text = "Save Responses"
OnClick="NextButton_Command"
Height="25"
Width="110"
runat="Server">
</asp:Button>
<asp:Button id="Page4Restart"
Text = "Retake Survey"
OnClick="BackButton_Command"
Height="25"
Width="110"
runat= "Server">
</asp:Button>
</asp:View>
</asp:MultiView>
</asp:Panel>
</form>
</body>
</html>
可是弄了半天没有弄好我要得效果。
一高手指点了一下用脚本写更好:
<head runat="server">
    <title>无标题页</title>
    <script language=javascript>
    function show(id)
    {
      if(id=="d1")
      {
        window.document.getElementById("d1").style.display ="block";
        window.document.getElementById("d2").style.display ="none";
       }
       else
       {
         window.document.getElementById("d2").style.display ="block";
         window.document.getElementById("d1").style.display ="none";
       }
     
    }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table width=100%><tr><td><font onclick="javascript:show('d1');">显示1</font><font onclick="javascript:show('d2');">显示2</font></td></tr>
        <tr>
            <td><div id=d1 style="display:block;">
           
    显示1
    </div>
    <div id=d2 style="display:none;">
    显示2
    </div>
            </td>
        </tr>
    </table>
   
    </form>
</body>
果然是,嘿嘿,我的问题总是这么多

posted @ 2007-07-27 16:02 Keren 阅读(85) | 评论 (4)编辑

2007年7月25日

全选gridView中的复选框


<asp:CheckBox ID="CheckAll" runat="server" Text="全选" OnCheckedChanged="selectAll" AutoPostBack="True" />

 protected void selectAll(object sender, EventArgs e)
    {
        //如果选中,则全选;否则,取消选择
        if (CheckAll.Checked == false)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)(GridView1.Rows[i].Cells[0].FindControl("ChBSelect"));
                chk.Checked = false;
            }
        }
        else
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)(GridView1.Rows[i].Cells[0].FindControl("ChBSelect"));
                chk.Checked = true;
            }
        }
    }

posted @ 2007-07-25 16:44 Keren 阅读(99) | 评论 (2)编辑

查询语句

关联表的写法
1.
select ... from a,b,c
where a... = b...
and c...= b...

2.
select * from (SELECT [hotelID], [hotelName], [hotelStar],
[CountryName]=(select C_Country_short from t_set_country where t_set_country.Country_Id=T_hotel.Country ),
[ProvinceName]=(select C_Province from t_set_province where t_set_province.Province_Id=T_hotel.Province ) ,
[CityName]=(select C_city from t_set_city where t_set_city.city_Id=T_hotel.City ),
[Postcode],[City],Country,Province, [hotelFax_Line], [hotelFaxCode], [hotelFax_AreaCode], [userCode], [hotelTele] FROM [T_hotel]) as tb";
where tb.Country='" + Request.QueryString["Id"] + "'

posted @ 2007-07-25 15:31 Keren 阅读(22) | 评论 (0)编辑

2007年7月23日

DUDU的签名,好

Talents come from diligence, and knowledge is gained by accumulation 天才源于勤奋,知识源于积累。

posted @ 2007-07-23 17:44 Keren 阅读(24) | 评论 (0)编辑

嘿嘿,终于申请到了博客园的博客

      我一直想要申请这个博课,把我学习的一些东西写进去,今天终于申请到了,嘿嘿......

posted @ 2007-07-23 15:29 Keren 阅读(25) | 评论 (0)编辑

导航

统计

与我联系

搜索

 

常用链接

留言簿

随笔分类

随笔档案

文章分类

相册

最新评论

阅读排行榜

评论排行榜