Check Directory & Read All Files One At A Time In C#

Hey guys,

I am trying to write an application that will do the following:

Check how many files (html) are in a folder
Read those html files
Display them one at a time in axWebBrowser.  

Basically it has to know how many files there are, load the intial one into the web browser I created, and then when the user clicks a button called Next (btnNext) it will load the 2nd html file into the same web browser (user can now see the 2nd one, but can't see the first unless they press Previous[btnPrevious])

At the minute I have this which isnt very good if the filenames/locations are different:
            System.Object nullObject = 0;
            string str = "";
            System.Object nullObjStr = str;
            Cursor.Current = Cursors.WaitCursor;
            axWebBrowser1.Navigate(@"C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\OpenFiles\Testing\bin\Debug\AUPs\test.html", ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
            Cursor.Current = Cursors.Default;
LVL 15
angus_young_acdcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

redpipeCommented:
I am not familiar with the WebBrowser component but the following method shows a way to get an array of htm/html files in a given folder:

private ArrayList GetWebFiles(string folderName)
{
  DirectoryInfo di = new DirectoryInfo(folderName);
  if (!di.Exists) {
    throw new DirectoryNotFoundException();
  }
  ArrayList result = new ArrayList();
  foreach (FileInfo fi in di.GetFiles("*.htm*")) {
    result.Add(fi.Name); // use fi.FullName if you need complete filereference
  }
  return result;
}
0
ozymandiasCommented:
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("path_goes_here");
FileInfo[] files = d.GetFiles("*.html");
0
angus_young_acdcAuthor Commented:
Hey guys,

thanks for the replies, though I am not sure how I can use them to display the information?

They are to be shown in a MS Explorer (active x webbrowser) called axWebBrowser.   Using either example how can I load the first HTML file into the browser automatically?   And then how do I change to another HTML file by clicking Next?   The HTML files will have pictures & text.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

ozymandiasCommented:
To load the first file you would do this :


int currentpos = 0;

void LoadCurrentFile(){
     axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);

}

void Next(){
    this.currentpos++;
    this.LoadCurrentFile();
}

void Previous(){
    this.currentpos--;
    this.LoadCurrentFile();

}
0
ozymandiasCommented:
You may want to check the logic so that currentpos cannot go beyond the length of the files array or be less than 0.
0
angus_young_acdcAuthor Commented:
Ok I have done a quick test by putting it in, (i made a new project and put all the code into Form1_Load).   I am getting a good few errors but I would say that solving one would do the trick.  The first error is:

The name 'd' does not exist in the current context

Any idea what I can do to solve this?
0
ozymandiasCommented:
Sorry, bug in my code :

System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("path_goes_here");
FileInfo[] files = d.GetFiles("*.html");

should be

System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("path_goes_here");
FileInfo[] files = directory.GetFiles("*.html");
0
angus_young_acdcAuthor Commented:
No worries mate, I'm definately stuck though.   I am getting "The name 'files' does not exist in the current context.  I must be putting the code in the wrong place like normal.

This is the code I have got in this little test:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication21
{



    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@"C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\AUPTestingPrototype\Testing\bin\Debug\AUPs");
            FileInfo[] files = directory.GetFiles("*.html");
        }
 
        void LoadCurrentFile()
        {
           
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);

        }

        void Next()
        {
            this.currentpos++;
            this.LoadCurrentFile();
        }

        void Previous()
        {
            this.currentpos--;
            this.LoadCurrentFile();

        }
    }
}

Am I meant to modify the LoadCurrentFile part?  As pressing '.' doesn't bring up any options for me to pick.   Sorry for the confusion
0
ozymandiasCommented:
You need to declare the fileInfo[] files at class level :

namespace WindowsApplication21
{



    public partial class Form1 : Form
    {

       FileInfo[] files;
       DirectoryInfo directory


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           directory = new System.IO.DirectoryInfo(@"C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\AUPTestingPrototype\Testing\bin\Debug\AUPs");
           files = directory.GetFiles("*.html");
        }
 
        void LoadCurrentFile()
        {
           
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);

        }

        void Next()
        {
            this.currentpos++;
            this.LoadCurrentFile();
        }

        void Previous()
        {
            this.currentpos--;
            this.LoadCurrentFile();

        }
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
angus_young_acdcAuthor Commented:
Cheers mate with a little tinkering I have it basically working now, last set of issues if that's ok?

How can I set it so that if there are no HTML files it won't give me an error?  Or that if I have been through all the files it will just look through them again, or the button becomes disabled or something.  
0
ozymandiasCommented:
OK.

         void LoadCurrentFile()
        {
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
            this.backButton.Enabled = (this.currentpos > 0);
            this.nextButton.Enabled = (this.currentpos < (this.files.Length -1));
        }

0
ozymandiasCommented:
The above will enable or diable the next and back button depending on what the current position of the file is within th fileinfo array.
You  could also do this :

void LoadCurrentFile()
        {
            if (files.Length == 0){
                 this.backButton.Enabled = false;
                 this.nextButton.Enabled = false;
                 MessageBox.Show(this,"No files found in directory","No files");
                 return;
            }
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
            this.backButton.Enabled = (this.currentpos > 0);
            this.nextButton.Enabled = (this.currentpos < (this.files.Length -1));
        }


0
angus_young_acdcAuthor Commented:
Cheers dude, all seems to work great now.  As I say I had to modify it a bit (didn't like ref nullObject) but that's down to me being a noob lol.  

Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.