我最近在做一个项目(C#,winform),要实现一个功能就是从客户端获取一段字符(文件夹名),然后与服务器上面的文件夹名称进行匹配查找,如果服务器上面有该文件夹不做任何处理,如果没有,则创建该文件夹!
问题是现在服务器上面有大批量的文件夹,而且还不断产生,我想用循环查找匹配,但是这样是不是效率太低了,所以请大家
帮忙看有没有什么好的思路方法??只说逻辑即可

解决方案 »

  1.   

    [code=C#]  DirectoryInfo dir = new DirectoryInfo(@"C:\aa\aa");
                if (!dir.Exists)
                {
                    dir.Create();
                }code]
      

  2.   

      DirectoryInfo dir = new DirectoryInfo(@"C:\aa\aa");
                if (!dir.Exists)
                {
                    dir.Create();
                }
      

  3.   

    那你做一个监视器监视目录好了,然后所有的目录信息都放在文件里,一条一个目录,检查这个文件里的目录列表里有没有就可以了
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace WindowsApplication244
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            FileSystemWatcher FSW = new FileSystemWatcher();
                FSW.Path = @"c:\";
                FSW.NotifyFilter = NotifyFilters.DirectoryName;
                FSW.Created += new FileSystemEventHandler(FSW_Created);
                FSW.Deleted += new FileSystemEventHandler(FSW_Deleted);
                FSW.Renamed += new RenamedEventHandler(FSW_Renamed);            FSW.EnableRaisingEvents = true;
            }        void FSW_Renamed(object sender, RenamedEventArgs e)
            {
                String S = File.ReadAllText(@"c:\save.txt");            S = S.Replace(e.OldFullPath + Environment.NewLine
                    , e.FullPath + Environment.NewLine);
                File.WriteAllText(@"c:\save.txt", S);            MessageBox.Show("当前C盘根目录下所有一级子目录: " + S);
            }        void FSW_Deleted(object sender, FileSystemEventArgs e)
            {
                if (!File.Exists(@"c:\save.txt"))
                    return;            String S = File.ReadAllText(@"c:\save.txt");            S = S.Replace(e.FullPath + Environment.NewLine, String.Empty);
                File.WriteAllText(@"c:\save.txt", S);            MessageBox.Show("当前C盘根目录下所有一级子目录: " + S);
            }        void FSW_Created(object sender, FileSystemEventArgs e)
            {
                String S = String.Empty;            if (!File.Exists(@"c:\save.txt"))
                    File.Create(@"c:\save.txt").Close();
                else
                    S = File.ReadAllText(@"c:\save.txt");            S += e.FullPath + Environment.NewLine;
                File.WriteAllText(@"c:\save.txt", S);            MessageBox.Show("当前C盘根目录下所有一级子目录: " + S);
            }
        }
    }
      

  4.   


    又是遍历,又有字符串匹配,所以,比较直接这个方案。
    而且,个人认为,这个索引如果不方便放到内存里的话,用数据库应该比较好,不需要自己写算法,用SQL语句就可以,速度也比较有保障。只是,需要注意一下与实际存储情况的同步。 
      

  5.   

    在服务器上找到与客户端传过来的文件夹名相同的文件夹,由于服务器上文件夹太多,我的问题是如何快速、高效的来查找到???
    虽然可以用FOR循环,但是由于文件夹太多,效率会不会太低???
      

  6.   

    1.建数据库,将文件夹名在sql table中保存。
    2.客户端查询时: 到数据库中查询(建索引),这样速度会非常快,而且不用担心数据量大小,几千万个数据量也是没有问题的