开发者

Unsure about passing an object to my class

开发者 https://www.devze.com 2023-01-04 13:55 出处:网络
My project has changed in scope.Originally, I had a path that was hardcoded into my app.Now, I have a UI that basically allows users to select which drive to drill into.It returns the path as a string

My project has changed in scope. Originally, I had a path that was hardcoded into my app. Now, I have a UI that basically allows users to select which drive to drill into. It returns the path as a string object. However, i am unsure how to implement this into my code.

Here is the code for the 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;
           开发者_StackOverflow if (retPath == null)
            {
                retPath = "";
            }
            return retPath;
        }
        else
        {
            return "";
        }
    }

}

How do i take that code and pass it into this code?

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

                }
            }
        }

    }

Also, how can i make it so that my console output ONLY has files that have invalid chars? Right now it shows ALL files that my app drills into.

Any help is appreciated--this project is a bit tough for me since i'm pretty new to this stuff!!!


Once your folder is selected, you'll need to save that string into a variable. You could call it selectedFolder. Then you'll want to replace all of your hard-coded directories, the @"S:\bob.smith\" instances, with selectedFolder.

selectedFolder = FolderSelect("Please select:");


Instead of returning the returnPath within the FolderSelect function you could save it to a variable within the class. This way the next part of your code can access it, either because it is within the class or through a getter. The variable can just contain the last pathway that the user specified or you can have an array that holds all the pathways if there will be more than one.

0

精彩评论

暂无评论...
验证码 换一张
取 消