Thread: FindFirstFile

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    FindFirstFile

    The 1:st Code, I have put inside a buttoncontrol. This does open a folderBrowserDialog. When I select a folder and press OK. The SelectedPatch is inserted in a textBox1.
    What I am trying to do here now is with the 2:nd code to choose a folder and when pressing OK here I want all files that is ".txt" to be inserted into this textBox1.
    I know I will have to use FindFirstFile, FindNextFile and FindClose in a way.
    I have tried out the first thing to Catch The first .txt file but this doesn´t compile.
    Any ideas what it could depend on.



    1:st
    Code:
    folderBrowserDialog1.ShowDialog();
    this.textBox1.Text = folderBrowserDialog1.SelectedPath;
    2:nd
    Code:
    Handle h;
    LPWIN32_FIND_DATA lpFileName;
    h = FindFirstFile(folderBrowserDialog1.SelectedPath, &lpFileName);
    			
    this.textBox1.Text = h;

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    May I add another suggestion for you?
    Don't use Native APIs. The .NET Framework should have everything you need for searching for files. Then you do not have to worry about interoperability.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Okay I really dont know the exact meening with Native APIs.

    Should I use the technique with FindFirstFile etc.. ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Coding View Post
    Okay I really dont know the exact meening with Native APIs.
    Everything in the .NET Framework is managed code. Everything C/C++ is native.

    Should I use the technique with FIndFirstFile etc.. ?
    No. In C# there are functions in the .NET Framework for that.
    FindFirstFile is for C/C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    If you are doing C# .NET, please please don't touch those Native APIs!

    hm, not sure of your problem but here is a function I wrote quick, hopefully it helps :

    Code:
            /*########################################*/
            /*########################################*/
            /*########################################*/
            public bool _Folder_GetFiles(string vSourceDirectory, out string[] vFileNames)
            {
                try
                {
                    int vRecursionDepth = 4;
                    int vHowDeepToScan = 4;
                    if (vRecursionDepth <= vHowDeepToScan)
                    {
                        string[] fileEntries = Directory.GetFiles(vSourceDirectory);
                        vFileNames = fileEntries;
                        return true;
                    }
                    else
                    {
                        vFileNames = null;
                        return false;
                    }
                }
                catch
                {
                    vFileNames = null;
                    return false;
                }
            }
            /*########################################*/
            /*########################################*/
            /*########################################*/
    ps. It's a short version of a recursive one I use which gets ALL files in ALL subfolders so that's why the code is a bit "weird" looking.


  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Thanks a lot I will check this code out and see what I can do.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Can I just ask, doesn't this work exactly the same?

    Code:
            public bool _Folder_GetFiles(string vSourceDirectory, out string[] vFileNames)
            {
                try
                {
                    vFileNames = Directory.GetFiles(vSourceDirectory);
                    return true;
                }
                catch
                {
                    vFileNames = null;
                    return false;
                }
            }
    Your if else will always be true anyway.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  8. #8
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Quote Originally Posted by DanFraser View Post
    Can I just ask, doesn't this work exactly the same?

    Code:
            public bool _Folder_GetFiles(string vSourceDirectory, out string[] vFileNames)
            {
                try
                {
                    vFileNames = Directory.GetFiles(vSourceDirectory);
                    return true;
                }
                catch
                {
                    vFileNames = null;
                    return false;
                }
            }
    Your if else will always be true anyway.
    Yes, well as I stated, my function is just a small cut off from a larger function. I pasted it quickly just to give him a hint so I had to cut off a lot of code from the function which you see there.


  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    No problem

    Just checking my own head there!
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FindFirstFile() Always Returns INVALID_HANDLE_VALUE
    By ThLstN in forum Windows Programming
    Replies: 10
    Last Post: 09-05-2008, 12:12 PM
  2. FindFirstFile() and std::string
    By Mostly Harmless in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2008, 06:27 PM
  3. FindFirstFile
    By Devil Panther in forum Windows Programming
    Replies: 10
    Last Post: 11-24-2006, 12:23 PM
  4. how do i use FindFirstFile()
    By knight543 in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2002, 08:00 PM
  5. Searching for a file using FindFirstFile() ?
    By Brian in forum Windows Programming
    Replies: 7
    Last Post: 01-27-2002, 02:43 PM