【问题标题】:I am unable to populate files from one folder我无法从一个文件夹中填充文件
【发布时间】:2014-11-04 20:21:54
【问题描述】:

我正在从两个不同的文件夹中填充文件并根据文件名进行排序。但我无法从一个指定的文件夹中填充。为什么会这样。我想我正在做一些错误。请帮帮我。

我的代码sn-p:

private void Form1_Load(object sender, EventArgs e)
    {
        //Initialize Directory path
        string draft = ini.ReadValue("Location", "Draft");
        string release = ini.ReadValue("Location", "Release");
        string drawing = ini.ReadValue("Location", "Drawing");//<<-- Unable to populate this location to the gridview.. Its taking the files from above location..
        string[] arrDraft = Directory.GetFiles(draft, "*", SearchOption.AllDirectories);
        string[] arrRelease = Directory.GetFiles(release, "*", SearchOption.AllDirectories);
        string[] arrDrawing = Directory.GetFiles(release, "*", SearchOption.AllDirectories);

        dt.Columns.Add("Part Number");
        dt.Columns.Add("Drawing");
        dt.Columns.Add("Draft Path");
        dt.Columns.Add("Release Path");
        dt.Columns.Add("Comment");
        dt.Columns.Add("Error");


        List<FileDetails> lst = new List<FileDetails>();
        foreach (string file in arrDraft)
        {
            Finder finder = new Finder(Path.GetFileName(file).Substring(0, 7));
            string abc = Array.Find(arrRelease, finder.Match);
            string def = Array.Find(arrDrawing, finder.Match);//<<--- Here is the mistake i guess i am doing
            dt.Rows.Add(Path.GetFileName(file), def, file, abc, String.Empty, String.Empty);
        }

        dataGridView1.DataSource = dt;
    }

    // Search predicate returns true if a string ends in "saurus".
    private static bool MatchFileName(String s, String _match)
    {
        return ((s.Length > 5) && (s.Substring(0, 7).ToLower() == _match.ToLower()));
    }

  public class FileDetails
    {
        public string FileName;
        public string Drawing;
        public string FilePathDraft;
        public string FilePathRelease;
        public string Comment;
        public string ErrorMsg;
    }
    public sealed class Finder
    {
        private readonly string _match;

        public Finder(string match)
        {
            _match = match.ToLower();
        }

        public bool Match(string s)
        {
            string fileName = s.Substring(s.LastIndexOf("\\") + 1);
            return ((fileName.Length > 5) && (fileName.Substring(0, 7).ToLower() == _match));
        }

【问题讨论】:

  • 你得到什么错误信息?

标签: c# wpf visual-studio-2010 visual-studio directory


【解决方案1】:

也许这一行

string[] arrDrawing = Directory.GetFiles(release, "*", SearchOption.AllDirectories);

应该是

string[] arrDrawing = Directory.GetFiles(drawing, "*", SearchOption.AllDirectories);

【讨论】:

    【解决方案2】:

    它应该与string name drawing 一起使用。你已经给了realease这就是为什么它会这样显示。

    请将绘图代码替换为

     string[] arrDrawing = Directory.GetFiles(drawing, "*", SearchOption.AllDirectories);
    

    一切就绪……!!!

    【讨论】:

    • @StacyKebler 始终确保变量名!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多