列出输出?

时间:2010-06-22 17:05:57

标签: c#

我有一个应用程序通过驱动器进行递归并输出具有“无效”字符的文件。现在我必须为它添加一个UI,我有点困惑。

在此作为WinForm应用程序之前,代码是(请注意,此STARTED作为控制台应用程序):

class Program
{
    public static void Main(string[] args)
    {


        //recurse through files.  Let user press 'ok' to move onto next step        
        string[] files = Directory.GetFiles(@"S:\bob.smith\", "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            Console.Write(file + "\r\n");
        }
        Console.WriteLine("Press any key to continue...");
        Console.ReadKey(true);
        //End section


        //Regex -- find invalid chars
        string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
        string replacement = " ";
        Regex regEx = new Regex(pattern);

        string[] fileDrive = Directory.GetFiles(@"S:\bob.smith\", "*.*", SearchOption.AllDirectories);
        List<string> filePath = new List<string>();

        //clean out file -- remove the path name so file name only shows

        foreach(string fileNames in fileDrive)
        {
        filePath.Add(fileNames);
        }

        using (StreamWriter sw = new StreamWriter(@"S:\bob.smith\File_Renames.txt"))
        {
            //Sanitize and remove invalid chars  
            foreach (string Files2 in filePath)
            {
                try
                {
                    string filenameOnly = Path.GetFileName(Files2);
                    string pathOnly = Path.GetDirectoryName(Files2);
                    string sanitizedFilename = regEx.Replace(filenameOnly, replacement);
                    string sanitized = Path.Combine(pathOnly, sanitizedFilename);
                    sw.Write(sanitized + "\r\n");
                    System.IO.File.Move(Files2, sanitized);

                }
                //error logging
                catch(Exception ex)
                {
                    StreamWriter sw2 = new StreamWriter(@"S:\bob.smith\Error_Log.txt");
                    sw2.Write("ERROR LOG");
                    sw2.WriteLine(DateTime.Now.ToString() + ex + "\r\n");
                    sw2.Flush();
                    sw2.Close();

                }
            }
        }

    }

现在我必须实现一个UI,我希望用包含无效字符的文件名填充ListView。我只想显示那些需要重命名的文件 - 而不是像现在这样的所有文件。我该怎么做并让它列出ListView中的这些文件?任何人都有可能有帮助的语法吗?

4 个答案:

答案 0 :(得分:1)

不像现在这样。您正在清理所有文件是否需要它们,而不是跟踪所做的事情。如果你使用正则表达式匹配包含字符的文件,并只将那些文件移动到需要重写的列表中:

foreach(string fileNames in fileDrive) 
{ 
    if(matchesregexp(filenames))
        filePath.Add(fileNames); 
}

然后修复这些文件的名称并将该列表输出到ListView。

答案 1 :(得分:1)

foreach(string fileNames in fileDrive)
{
    if(regex.IsMatch(fileNames)) //check that it has invalid characters
    {
        filePath.Add(fileNames);
        yourListView.Items.Add(fileNames); //Add the string to your list view
    }
}

答案 2 :(得分:1)

您可以将其添加到现有代码中:

    using (StreamWriter sw = new StreamWriter(@"S:\bob.smith\File_Renames.txt")) 
    { 
        //Sanitize and remove invalid chars   
        foreach (string Files2 in filePath) 
        { 
            try 
            { 
                if (regex.Match(Files2).Success) 
                {
                    Label l = new Label();
                    l.Content = Files2;
                    listview.Items.Add(l);
                }
                string filenameOnly = Path.GetFileName(Files2); 
                string pathOnly = Path.GetDirectoryName(Files2); 
                string sanitizedFilename = regEx.Replace(filenameOnly, replacement); 
                string sanitized = Path.Combine(pathOnly, sanitizedFilename); 
                sw.Write(sanitized + "\r\n"); 
                System.IO.File.Move(Files2, sanitized); 

            } 
            //error logging 
            catch(Exception ex) 
            { 
                StreamWriter sw2 = new StreamWriter(@"S:\bob.smith\Error_Log.txt"); 
                sw2.Write("ERROR LOG"); 
                sw2.WriteLine(DateTime.Now.ToString() + ex + "\r\n"); 
                sw2.Flush(); 
                sw2.Close(); 

            } 
        } 
    }

答案 3 :(得分:1)

你能用这段代码吗?

        ListView blah = new ListView();
        this.Controls.Add(blah);
        blah.Location = new Point(0, 0);
        blah.Items.Add("hello");

如果是这样,您应该可以使用上面的代码。