0

I am converting a code from c++ to c#. The code uses the function
findFirstFile() to return the first file of the directory..

Is this function present in c#, or there are any modifications to the
function..The function uses a header windows.h. What is its replacement in c#?

MSDN has no example for this function in c# ..
plz help.....

4
Contributors
3
Replies
5
Views
9 Years
Discussion Span
Last Post by bmdeveloper
0

I don't know if this function exists, but you can always make your own like this:

Something like a dir-command in DOS

using System;
using System.IO;

class MyProg
{
    static void Main(string[] args) 
    {
        DirectoryInfo dir = new DirectoryInfo(@"c:\");
        FileInfo[] files = dir.GetFiles(); //return files of the c dir

        foreach (FileInfo file in files) //look for first file in files array
       {
            Console.Write("Name: " + file.Name + "  ");
            Console.WriteLine("Size: " + file.Length.ToString()); //etc.
        }
    }
}
0

This example will find the 1st file in a directory. Edit as necessary:

using System.IO;

string dirname = "images/"; //edit this line.
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(dirname));
FileInfo[] pics = dirInfo.GetFiles("*.*"); //use *.jpg for jpg files only, etc.

int cont = 0;

foreach (FileInfo fi in pics)
{
    if (cont == 0)
    {
        label_firstfile.Text = fi.Name;
        cont++;
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.