using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Configuration;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Directory
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            DirectoryInfo di 
= new DirectoryInfo("D:/Image");//strBaseDir是起始目录,绝对地址
            DirectoryInfo[] diA = di.GetDirectories();//获得了所有一级子目录
            FileInfo[] fiA = di.GetFiles();//获得了所有起始目录下的文件

            
//最后,如何把所有目录信息从ArrayList中取出来呢?如下:
            for (int i = 0; i < diA.Length; i++)
            
{
                DirectoryInfo db 
= new DirectoryInfo("D:/Image/"+diA[i].Name);
                FileInfo[] fdb 
= db.GetFiles();
                
for (int h = 0; h < fdb.Length; h++{
                    textBox1.AppendText(
"D:/Image/"+diA[i].Name+"/"+fdb[h]+ " ");
                    textBox1.AppendText(diA[i].Name 
+ " ");
                    textBox1.AppendText(fdb[h].Length 
+ " ");
                    
this.AddFile(diA[i].Name, "/Image/" + diA[i].Name + "/" + fdb[h], fdb[h].Length);
                }

                   
              
//textBox1是容器,拷贝我的代码,注意要换一个你自己的容器
            }
 

        }

        
        
//我把ArrayList当成动态数组用,非常好用
        public void GetAllDirList(string strBaseDir)
        
{
          DirectoryInfo di
=new DirectoryInfo(strBaseDir);
          DirectoryInfo[] diA
=di.GetDirectories();
          
for(int i=0;i<diA.Length;i++)
          
{
          
//al.Add(diA.FullName);
          
//diA.FullName是某个子目录的绝对地址,把它记录在ArrayList中
         
//GetAllDirList(diA.FullName);
          
//注意:递归了。逻辑思维正常的人应该能反应过来
          }

        }

        
public void AddFile(string Name, string Path, long size)
        
{

            FileStream myWStream 
= new FileStream("d:/test.txt", FileMode.Append, FileAccess.Write);
            System.IO.BinaryWriter binWrit 
= new BinaryWriter(myWStream);

            binWrit.Write(Name.ToString() 
+ "," + Path.ToString() + "," + size.ToString() + " ");
            binWrit.Close();
            myWStream.Close();

        }

        
public Boolean ExecuteSqlWrite(string SqlString)
        
{
            SqlCommand SqlCmd 
= new SqlCommand();

             
             
//ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();SqlCmd.Connection = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings[].ToString); System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString
            SqlCmd.Connection =  new SqlConnection(ConfigurationManager.ConnectionStrings["Directory.Properties.Settings.PicConnectionString"].ConnectionString);        
            SqlCmd.CommandText 
= SqlString;
            SqlCmd.CommandType 
= CommandType.Text;

            SqlCmd.Connection.Open();
            SqlCmd.ExecuteNonQuery();
            SqlCmd.Connection.Close();
            
return true;

        }

        
public void InsertDB(string Name,string Path,long size) 
        
{

            
//String SqlString = "insert Pic (Name,Url,Size) values ('" + Name + "','" + Path + "','" + size + "')";
            String SqlString = "insert Pic (Name,Url,Size) values ('bubul','/33/3','2222')";
            ExecuteSqlWrite(SqlString);
        
        }

        
    }

}
 

本文转载:CSDN博客