关于文件流的一些总结

System.io下的各种类在开发中还是比较重要的,我发点时间把自己的一点意见和总结弄了一下
System.IO 命名空间包含允许读写文件和数据流的类型以及提供基本文件和目录支持的类型。


文件的创建
 string path = Page.Server.MapPath(@"../upload/img/")+this.TextBox2.Text.Trim().ToString();
        
if (!File.Exists(path))
        
{
            File.Create(path);
        }

目录的创建
string path = Page.Server.MapPath(@"../upload/img/")+this.TextBox2.Text.Trim().ToString();
                
if (!Directory.Exists(path))
        
{
            Directory.CreateDirectory(path);
        }


  文件的重命名
try
        
{
            
string name2 = this.TextBox1.Text.Trim().ToString();
            
string name1 = this.DropDownList1.SelectedValue.ToString();
            
string path = Page.Server.MapPath(@"../upload/"+ name1;
            
string path2 = Page.Server.MapPath(@"../upload/"+ name2;
            System.IO.File.Move(path, path2);
            
if (File.Exists(path2))
            
{
                Response.Write(
"<script language='javascript'>alert('文件重命名成功');location.href('../filetest/Default.aspx');</script>");
            }

        }

        
catch (Exception ex)
        
{
            Debug.Fail(ex.Message);
        }
文件和目录的查找
try
        
{
            
//首先清楚下拉框的内容
            this.DropDownList1.Items.Clear();
            
string path = Page.Server.MapPath(@"../upload/");
            DirectoryInfo theFolder 
= new DirectoryInfo(path);
            
foreach (DirectoryInfo nextFolder in theFolder.GetDirectories())
            
{
                
this.DropDownList1.Items.Add(nextFolder.ToString());
            }

            
foreach (FileInfo nextFile in theFolder.GetFiles())
            
{
                
this.DropDownList1.Items.Add(nextFile.Name.ToString());
            }

        }

        
catch (FileNotFoundException ex)
        
{
            Debug.Fail(ex.Message);
        }

        
catch (DirectoryNotFoundException ex2)
        
{
            Debug.Fail(ex2.Message);
        }
还有其他的我已经在另外的文章中介绍了,这里就不重复了! 

posted on 2008-07-05 15:24  小顾问  阅读(349)  评论(0)    收藏  举报