Solved

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

Posted on 2006-11-14
13
250 Views
Last Modified: 2010-04-16
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
Comment
Question by:angus_young_acdc
  • 7
  • 5
13 Comments
 
LVL 8

Expert Comment

by:redpipe
ID: 17937672
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 17937695
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("path_goes_here");
FileInfo[] files = d.GetFiles("*.html");
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17938190
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:ozymandias
ID: 17938470
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 17938477
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
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17938514
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 17938553
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
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17938615
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
 
LVL 15

Accepted Solution

by:
ozymandias earned 75 total points
ID: 17938627
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
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17938742
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 17938811
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 17938837
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
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 17938884
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

809 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