显示当前目录下的每个文件的名称

 #region 用DirectoryInfo这个类实现查看目录下的每个文件
    public void LookDirectoryInfo()
    {
        string directoryName = @"c:\tem";
        DirectoryInfo directoryInfo = new DirectoryInfo(directoryName);

 

        FileInfo[] fileList = null;
        if (!directoryInfo.Exists)//如果不存在创建目录
        {
            directoryInfo.Create();
        }
        fileList = directoryInfo.GetFiles();
        //foreach (FileInfo file in fileList)
        //{
        //    ListBox1.Items.Add(file);
        //}
        ListBox1.DataSource = fileList;
        ListBox1.DataBind();

    }
    #endregion

创建文件和创建目录一样的,我这里用的实例类DirectoryInfo

 

 #region 用静态类实现
    private void LookDirectory()
    {
        string directoryName = @"c:\tem";
        if(!Directory.Exists(directoryName))
        {
            Directory.CreateDirectory(directoryName);

 

        }
        string[] fileList = Directory.GetFiles(directoryName);
        ListBox1.DataSource = fileList;
        ListBox1.DataBind();
    }
    #endregion

你可能感兴趣的:(文件)