use a variable string for GetFiles method

Posted on 2011-05-01
Last Modified: 2012-05-11
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))

Directly above here is the foreach statement with the Getfiles call.  Is this just a syntax problem?
Question by:antarctican69
    LVL 19

    Expert Comment

    Are you aware that you put a sleep of 1 ms , if you want 1 sec you should put a 1000 value ....
    LVL 85

    Accepted Solution

    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;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now