• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

use a variable string for GetFiles method

OK, I am using VS 2010 C#, and I am trying to use a file path (e.g. C:/Desktop/blahblah) as a string for the GetFiles method, so that once a folder is chosen from a dialog, the path to that folder will be passed to GetFiles, and the logic will happen.  Here is the relevant snippet.
// Bring up a dialog to open a file.
        private void button2_Click(object sender, EventArgs e)
        {
            bool fileOpened = false;

            FolderBrowserDialog filePath = new FolderBrowserDialog();


            // If a file is not opened, then set the initial directory to the
            // FolderBrowserDialog.SelectedPath value.
            if (!fileOpened)
            {
                folderBrowserDialog1.SelectedPath = @"C:\Users\Giles Kingsley\Desktop\ProjectImages";
             

            }
            // Display the openFile dialog.
            DialogResult result = folderBrowserDialog1.ShowDialog();

                        // OK button was pressed.
            if (result == DialogResult.OK)
            {
                string openFileName = filePath.SelectedPath;
            }





        public void StartUpdate()
        {
            foreach (string img in Directory.GetFiles(openFileName))
            {
                LUBits(img);
                Thread.Sleep(1);
            }
        }

Directly above here is the foreach statement with the Getfiles call.  Is this just a syntax problem?
0
antarctican69
Asked:
antarctican69
1 Solution
 
elimesikaCommented:
Are you aware that you put a sleep of 1 ms , if you want 1 sec you should put a 1000 value ....
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You're "openFileName" variable has been declared local to the button2 click event and therefore goes out of scope.  If you already have a variable of the same name declared at the class level then get rid of the "string" declaration and just assign it:

        private string openFileName;

        private void button2_Click(object sender, EventArgs e)
        {
            ...

            if (result == DialogResult.OK)
            {
                openFileName = filePath.SelectedPath;
            }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now