新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → Find Files In C# 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2759 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: Find Files In C# 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18407
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 Find Files In C#


    发信人: Jobs (温少), 信区: DotNET        
    标  题: Find Files In C#
    发信站: BBS 水木清华站 (Thu May 10 20:22:58 2001)


    示例代码:
    http://sunrise.szu.edu.cn:8096/Article%20Source%20Code/FindFileInCS.zip

    Find Files In C#  

    Submitted By User Level Date of Submission  
    Bejoy Nair Beginner 05/01/2001  
      
       
    I have developed a small application in c#, which helps in searching u'r
    hard disk for files of the desired extention. I have used System.IO  
    namespace from the .Net framework. So now gone are the days of importing
    the API's functions in VB or using the fileSystemObject(and adding  
    one more activex component with u'r builds!!!!!) for searching for doing
    file operations.The System.IO namespace gives us very effective classes
    for file specific operations.  

    Attached Code: FindFileInCS.zip 6 KB

    Source Code:

    //Author : Bejoy Nair
    //Company : Patni Computers,Pune.
    //Dated : 04-27-2001.
    //Comments: This is an application written in C# for searching files  
    on u'r hard drives.
    // The namespaces used are System.IO,System.Drawing and System.
    Winforms.
    // To compile the code give the below compiler options---
    // csc /r:System.dll /r:System.WinForms.dll /r:System.Drawing.dll /r:
    Microsoft.Win32.Interop.dll FindFile.cs
    //Tools : TextPad and .Net S.


    using System;
    using System.WinForms;
    using System.Drawing;
    using System.IO;


    public class frmFind : Form
    {

    private int nItems=0;
    static ListBox listBox1=new ListBox();
    static ComboBox cmbDrives= new ComboBox();
    static TextBox txtSearchType= new TextBox();
    static Label lblFileType = new Label();
    static Label lblFolder = new Label();
    static Label lblHeader = new Label();

    BorderStyle x= new BorderStyle();
    Button button1 = new Button();
    Button button2 = new Button();



    public frmFind()
    {
    this.Text="File Search Application using C#";
    this.EventHandler();  
    }

    // Create the form and add the required buttons.
    public void InitializeForm() {
    this.BorderStyle = FormBorderStyle.FixedDialog;
    this.MaximizeBox = false;  
    this.button1.Text="Get Data";
    this.button2.Text="Exit";
    this.BackColor=Color.White;

    this.button1.Location=new Point(70,245);
    this.button2.Location=new Point(this.button1.Left + this.button1.Left  
    + 30,this.button1.Top);
    this.button1.BackColor=Color.DeepSkyBlue;
    this.button2.BackColor=Color.DeepSkyBlue;
    this.button1.TabIndex=3;  


    // set the location of the Label.
    // lblFileType.BorderStyle = BorderStyle.None;
    lblFileType.Text ="File Type:";
    lblFileType.Location=new Point(10,30);

    lblFolder.Text ="Look In:";
    lblFolder.Location=new Point(10,60);

    lblHeader.Text="Select the drive and Enter the file type to be  
    searched!";
    lblHeader.AutoSize=true;
    lblHeader.Location=new Point(20,5);

    // Set the location of the ListBox.
    listBox1.Location=new Point(70,90);
    listBox1.BackColor=Color.Aqua;
    listBox1.Width=200;
    listBox1.Height=150;
    listBox1.HorizontalScrollbar=true;


    // Set the location of the comboBox.
    cmbDrives.Location=new Point(70,60);
    cmbDrives.BackColor=Color.Aqua;
    cmbDrives.Width=200;

    // Set the location of the search text field.
    txtSearchType.Location=new Point(70,30);
    txtSearchType.BackColor=Color.Aqua;
    txtSearchType.Width=200;
    txtSearchType.TabIndex=0;





    //Add the button to the form.
    this.Controls.Add(this.button1);
    this.Controls.Add(this.button2);  
    this.Controls.Add(listBox1);
    this.Controls.Add(cmbDrives);  
    this.Controls.Add(txtSearchType);
    this.Controls.Add(lblFileType);
    this.Controls.Add(lblFolder);
    this.Controls.Add(lblHeader);
    this.StartPosition = FormStartPosition.CenterScreen;
    }


    public void AddComboBoxItem(String [] strDrives){
    // listBox1.InsertItem(nItems,oItem);
    for(int i=0;i<strDrives.Length;i++) {
    cmbDrives.Items.Add(strDrives[i]);
    ++nItems;

    }
    }  

    public void AddListBoxItem(){
    listBox1.InsertItem(0,cmbDrives.Text);
    }

    public static int Main(string[] args)
    {
    frmFind frmFindApp= new frmFind();


    // Create the form.
    frmFindApp.InitializeForm();

    // Get the logical drives for the machine.
    string [] strDrives=frmFindApp.getDrives();
    frmFindApp.AddComboBoxItem(strDrives);




    // Display the form as a modal dialog box.
    frmFindApp.ShowDialog();

    return 0;
    }

    // Event Handler for buttons for getting the folders.  
    protected void button1_Click(object sender,System.EventArgs e) {
    string strMessage="No Matches";
    listBox1.Items.Clear();

    if((txtSearchType.Text=="")&&(cmbDrives.Text=="")){
    lblHeader.Text="Select the drive and Enter the file type to be  
    searched!";
    }  

    else if(txtSearchType.Text==""){
    lblHeader.Text="Enter the file type to be searched!";
    }

    else if(cmbDrives.Text==""){
    lblHeader.Text="Select the drive to search the files!";
    }


    else {  
    try{
    int i=0;  
    int iVal;
    Directory []ChildDirs= this.getDirectories(cmbDrives.Text);

    //If the Search file type has no extention then add one.
    iVal=txtSearchType.Text.IndexOf(".");
    if(iVal==-1){
    txtSearchType.Text+=".*";
    }

    //Get the Child Directories.
    foreach(Directory ChildDir in ChildDirs) {

    //recurse through the child directories.
    while(ChildDir.GetDirectories().Length>0){

    Directory []GrandChilds=ChildDir.GetDirectories();  

    foreach(Directory GrandChild in GrandChilds) {
    File [] Files = GrandChild.GetFiles(txtSearchType.Text);
    if(Files.Length==0){
    // Do nothing.  

    }
    else{  
    foreach(File DirFile in Files) {
    listBox1.InsertItem(i,DirFile.Name);
    i++;

    }
    }  

    ChildDir=GrandChild;  
    }



    }  

    }
    }catch(IOException E){
    strMessage=E.Message;
    listBox1.InsertItem(0,strMessage);
    strMessage="";
    }

    if (listBox1.Items.Count==0){
    listBox1.InsertItem(0,strMessage);

    }
    }  


    }  


    public String [] getDrives(){
    return Directory.GetLogicalDrives();
    }


    public Directory [] getDirectories(String strDrive) {
    Directory dir= new Directory(strDrive);
    Directory [] childDirs=dir.GetDirectories();
    return childDirs;
    }



    protected void button2_Click(object sender,System.EventArgs e) {
    //Close the list box.
    this.Close();


    }  


    protected void cmbDrives_SelectedIndexChanged(object sender,System.
    EventArgs e) {

    }



    //register the event handler.
    public void EventHandler() {

    this.button1.Click+=new System.EventHandler(this.button1_Click);
    this.button2.Click+=new System.EventHandler(this.button2_Click);
    cmbDrives.SelectedIndexChanged+=new System.
    EventHandler(cmbDrives_SelectedIndexChanged);

    }

    }  
      

    --

    ※ 来源:·BBS 水木清华站 smth.org·[FROM: 210.39.3.110]
    上一篇
    返回上一页
    回到目录
    回到页首
    下一篇


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/7/20 18:28:28

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    46.875ms