2011年7月12日 星期二

C# 抓取資料夾檔案資訊存放在DataTable,並將檔案複製到其他位置

抓取資料夾檔案資訊存放在DataTable
        DataTable DT = new DataTable();
DT.Columns.Add("FullName");

foreach (string fname in System.IO.Directory.GetFileSystemEntries("D:\\WWW"))
{
if (fname.IndexOf(".doc") > 0 || fname.IndexOf(".docx") > 0)
{
DT.Rows.Add(fname);
}
}

GridView1.DataSource = DT;
GridView1.DataBind();

抓取資料夾完整檔案資訊存放在DataTable
        DataTable DT = new DataTable();
DT.Columns.Add("Sno");
DT.Columns.Add("Name");
DT.Columns.Add("FullName");
DT.Columns.Add("Extension");
DT.Columns.Add("Length");
DT.Columns.Add("Attributes");
DT.Columns.Add("CreationTime");
DT.Columns.Add("LastAccessTime");
DT.Columns.Add("LastWriteTime");
DT.Columns.Add("Directory");

int Sno = 1;
foreach (string FilePathName in System.IO.Directory.GetFileSystemEntries("D:\\WWW", "*.doc"))
{
//FileInfo 請 using System.IO;
FileInfo theFile;
theFile = new FileInfo(FilePathName);

if (theFile.Exists)
{
DT.Rows.Add(Sno, theFile.Name, theFile.FullName, theFile.Extension, theFile.Length, theFile.Attributes.ToString(), theFile.CreationTime, theFile.LastAccessTime, theFile.LastWriteTime, theFile.Directory);
Sno = Sno + 1;
}
}

GridView2.DataSource = DT;
GridView2.DataBind();

抓取資料夾MP3檔案資訊,並透過WindowsMediaPlayer抓取檔案資訊,存放在DataTable
        if (FileUpload1.PostedFile.ContentLength > 0)
{
DataTable DT = new DataTable();
DT.Columns.Add("Sno");
DT.Columns.Add("Name");
DT.Columns.Add("FullName");
DT.Columns.Add("Extension");
DT.Columns.Add("Directory");
DT.Columns.Add("Length");
DT.Columns.Add("出版商");
DT.Columns.Add("演唱者");
DT.Columns.Add("歌曲名稱");


foreach (string FilePathName in System.IO.Directory.GetFileSystemEntries(FileUpload1.PostedFile.FileName.Replace(FileUpload1.FileName, "")))
{
if (FilePathName.IndexOf(".mp3") > 0 || FilePathName.IndexOf(".wma") > 0)
{
//FileInfo 請 using System.IO;
FileInfo theFile;
theFile = new FileInfo(FilePathName);

if (theFile.Exists)
{
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
wmp.settings.mute = true;
wmp.URL = theFile.FullName;
wmp.controls.play();
System.Threading.Thread.Sleep(100);

DT.Rows.Add(theFile.Name.Substring(0, 2), theFile.Name, theFile.FullName, theFile.Extension, theFile.Directory, theFile.Length, wmp.currentMedia.getItemInfo("Author"), wmp.currentMedia.getItemInfo("Artist"), wmp.currentMedia.getItemInfo("Title"));

wmp.close();
}
}
}

GridView2.DataSource = DT;
GridView2.DataBind();
}

將抓取到的資料檔案,複製檔案到其他位置(路徑)
        for (int i = 0; i < GridView2.Rows.Count; i++)
{
//原始檔案資料夾
String SourcePath = GridView2.Rows[i].Cells[9].Text + "\\" + GridView2.Rows[i].Cells[1].Text;
//要移至的檔案資料夾
String MoveToPath = "D:\\WWW\\" + GridView2.Rows[i].Cells[1].Text;

if (File.Exists(SourcePath))
{
File.Copy(SourcePath, MoveToPath, true);
}
}

        for (int i = 0; i < GridView2.Rows.Count; i++)
{
//原始檔案資料夾
String SourcePath = GridView2.Rows[i].Cells[9].Text + "\\" + GridView2.Rows[i].Cells[1].Text;
//要移至的檔案資料夾
String MoveToPath = "/RaWMroot/" + GridView2.Rows[i].Cells[1].Text;

if (File.Exists(SourcePath))
{
File.Copy(SourcePath, Server.MapPath(MoveToPath), true);
}
}

抓取上傳檔案完整路徑及名稱
Label1.Text = FileUpload1.PostedFile.FileName;

抓取上傳檔案路徑
Label1.Text = FileUpload1.PostedFile.FileName.Replace(FileUpload1.FileName, "");

參考網址 http://program.maomo.info/article.aspx?uid=64

沒有留言:

張貼留言