用户如何在控制台上选择文件夹? [英] How do I the user select folder on the console ?

查看:56
本文介绍了用户如何在控制台上选择文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在控制台上使用PathA和PathB代替用户选择路径?



代码=



how can i do PathA and PathB instead of User select paths on the console ?

Code =

{
    class Program
    {
        static void Main(string[] args)
        {
            string pathA = "";
            string pathB = "";
            string ignoreListFile = @"C:\IgnoreList.txt";
            string[] ignoreList = File.ReadAllLines(ignoreListFile);

            String ignoredExtension = "";

            foreach (var extension in ignoreList)
            {
                ignoredExtension = ignoredExtension + extension;
            }

            System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
            System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);

            IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
            IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

            FileCompare myFileCompare = new FileCompare();

            Excel.Application excel = new Excel.Application();
            excel.Visible = true;
            object Missing = Type.Missing;
            Workbook workbook = excel.Workbooks.Add(Missing);
            Worksheet sheet1 = (Worksheet)workbook.Sheets[1];

            int sheetRowNumber = 1;

            Range lineNumberHeaderRange = (Range)sheet1.Cells[sheetRowNumber, 1];
            lineNumberHeaderRange.Value = "Line Number";
            lineNumberHeaderRange.EntireRow.Font.Bold = true;

            var queryCommonFiles = list1.Intersect(list2, myFileCompare);
            var queryCommonFiles2 = list2.Intersect(list1, myFileCompare);

            if (queryCommonFiles.Count() > 0)
            {
                String fileName1 = "";
                String fileName2 = "";

                foreach (var file1 in queryCommonFiles)
                {

                    if (ignoredExtension.IndexOf(file1.Extension) > -1)
                        continue;

                    fileName1 = file1.Name;

                    foreach (var file2 in queryCommonFiles2)
                    {
                        fileName2 = file2.Name;
                        if (fileName1 == fileName2)
                        {

                            int StartCol = 1;
                            //int StartRow = 1;


                            string[] fileLines1 = File.ReadAllLines(file1.FullName);
                            string[] fileLines2 = File.ReadAllLines(file2.FullName);

                            int count = fileLines1.Length;
                            //int j = 1;
                            int p;
                            p = StartCol + fileLines2.Length;

                            bool firstDifferenceFound = false;

                            for (int i = 0; i < count; i++)
                            {
                                if (fileLines2.Length > i && fileLines1[i] != fileLines2[i])
                                {

                                    if (!firstDifferenceFound)
                                    {
                                        firstDifferenceFound = true;

                                        if (sheetRowNumber > 1)
                                        {
                                            sheetRowNumber++;
                                        }

                                        Range fileName1Range = (Range)sheet1.Cells[sheetRowNumber, 2];
                                        fileName1Range.Value = fileName1.ToString() + "==>" + file1.FullName;
                                        fileName1Range.EntireRow.Font.Underline = true;
                                        fileName1Range.EntireRow.Font.Bold = true;

                                        Range fileName2Range = (Range)sheet1.Cells[sheetRowNumber, 3];
                                        fileName2Range.Value = fileName2.ToString() + "==>" + file2.FullName; ;
                                        fileName2Range.EntireRow.Font.Underline = true;
                                        fileName2Range.EntireRow.Font.Bold = true;
                                    }

                                    Range lineNumberRange = (Range)sheet1.Cells[sheetRowNumber + 1, 1];
                                    lineNumberRange.Value = (i + 1).ToString();
                                    lineNumberRange.Font.Bold = true;

                                    Range line1Range = (Range)sheet1.Cells[sheetRowNumber + 1, 2];
                                    line1Range.Value = fileLines1[i].ToString();
                                    line1Range.EntireRow.Font.Bold = false;
                                    line1Range.EntireRow.Font.Underline = false;

                                    Range line2Range = (Range)sheet1.Cells[sheetRowNumber + 1, 3];
                                    line2Range.Value = fileLines2[i].ToString();
                                    line2Range.EntireRow.Font.Bold = false;
                                    line2Range.EntireRow.Font.Underline = false;

                                    sheetRowNumber++;
                                }
                            }

                           
                            break;
                        }
                    }
                }

         
            }
        }
    }
}

class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
{
    public FileCompare() { }

    public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
    {
        return (f1.Name == f2.Name);
    }

    public int GetHashCode(System.IO.FileInfo fi)
    {
        string s = String.Format("{0}", fi.Name);
        return s.GetHashCode();
    }
}

推荐答案

你能否通过路径A&命令行中的pathB值 - 有很多命令行解析器 - 我个人喜欢使用一个允许我在类中指定参数并使用自定义属性来定义参数的特征



顺便说一句,如果你的路径中有空格,最好在路径周围使用报价



我还包括一个这样的这里的库,CodeProject也有一个数字...... 命令行解析器库 - 主页 [ ^ ]



如果你可以考虑的其他事项不希望使用CommandLine参数 - 使用App.Config并从那里读取值 - 或使用App.Config&中的两个默认值。允许来自命令行[/ Edit]
Could you pass the pathA & pathB values in on the command-line - there are many command-line parsers out there - personally I like to use one that allows me to specify parameters in a class and use custom attributes to define the characteristics of the arguments

btw, if your paths have spaces in the them, its best to use quotes around the paths

Ive included one such Library here, CodeProject has a number as well ... Command Line Parser Library - Home[^]

Other things you may consider if you dont wish to use CommandLine arguments - use App.Config and read the values from there - or use both, default values in App.Config & allow overrides from the Command Line [/Edit]


这篇关于用户如何在控制台上选择文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆