Solved

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

Posted on 2006-11-14
13
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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