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

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;
0
angus_young_acdc
Asked:
angus_young_acdc
  • 7
  • 5
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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