不确定将对象传递给我的班级

时间:2010-06-22 14:08:04

标签: c#

我的项目范围发生了变化。最初,我有一条硬编码到我的应用程序的路径。现在,我有一个UI,基本上允许用户选择要钻进的驱动器。它将路径作为字符串对象返回。但是,我不确定如何将其实现到我的代码中。

以下是用户界面的代码:

private void button1_Click(object sender, EventArgs e)
    {
        FolderSelect("Please select:");
    }
    private static string FolderSelect(string txtPrompt)
    {

        //Now, we want to use the path information to population our folder selection initial location
        string initialCheckoutPathDir = (@"C:\"); 
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(initialCheckoutPathDir);
        FolderBrowserDialog FolderSelect = new FolderBrowserDialog();
        FolderSelect.SelectedPath = info.FullName;
        FolderSelect.Description = txtPrompt;
        FolderSelect.ShowNewFolderButton = true;
        if (FolderSelect.ShowDialog() == DialogResult.OK)
        {
            string retPath = FolderSelect.SelectedPath;
            if (retPath == null)
            {
                retPath = "";
            }
            return retPath;
        }
        else
        {
            return "";
        }
    }

}

如何获取该代码并将其传递给此代码?

//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();

                }
            }
        }

    }

另外,我怎样才能使我的控制台输出只有具有无效字符的文件?现在它显示我的应用程序钻进的所有文件。

任何帮助都表示赞赏 - 这个项目对我来说有点难度,因为我对这些东西很陌生!!!

2 个答案:

答案 0 :(得分:0)

选择文件夹后,您需要将该字符串保存到变量中。你可以称之为selectedFolder。然后,您将要使用selectedFolder替换所有硬编码目录,即@“S:\ bob.smith \”实例。

selectedFolder = FolderSelect("Please select:");

答案 1 :(得分:0)

您可以将其保存到类中的变量,而不是在FolderSelect函数中返回returnPath。这样,代码的下一部分可以访问它,因为它在类中或通过getter。变量可以只包含用户指定的最后一个路径,或者如果存在多个路径,则可以包含一个包含所有路径的数组。

相关问题