9 Replies - 786 Views - Last Post: 11 July 2012 - 05:27 PM Rate Topic: -----

#1 persson121   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 30-June 12

How do i get all the files names who are in the folder?

Posted 11 July 2012 - 02:15 PM

Hi.

We say that i have X files in my folder.
The files have allmust the same name.
The name of the files is: bookmark_?.ini
bookmark_??.ini
bookmark_???.ini
bookmark_????.ini
and so on.

But i don't know the hole whole names of the files.
I want to put them into a list but i wonder how i can get all the files names who are in the folder without knowing what the names are?

This post has been edited by tlhIn`toq: 11 July 2012 - 02:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do i get all the files names who are in the folder?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14094
  • View blog
  • Posts: 56,472
  • Joined: 12-June 08

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 02:28 PM

Typically io.directory.getfiles.

http://msdn.microsof...y.getfiles.aspx
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,461
  • Joined: 05-May 12

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 02:48 PM

Why not use the '*' wildcard instead? "bookmark_*.ini"
Was This Post Helpful? 0
  • +
  • -

#4 persson121   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 30-June 12

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 03:20 PM

This

string[] files_names = Directory.GetFiles(strPath, "*.ini", SearchOption.TopDirectoryOnly);


Was This Post Helpful? 0
  • +
  • -

#5 persson121   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 30-June 12

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 03:54 PM

No. It didin't work!

I did use this code:
string[] filenames = Directory.GetFiles(@"C:\bookmarks\", "*.ini", SearchOption.TopDirectoryOnly);
            
            List<string> lines_new = new List<string>(filenames);
            MessageBox.Show(filenames[0]);



And the output:
C:\bookmarks\COM.ini

i Want ONLY
COM
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 04:08 PM

Personally, I prefer the DirectoryInfo and FileInfo objects. They return more useful objects than just lists of strings. In this case, if you use DirectoryInfo to search for these files, it returns an array of FileInfos, which have properties that have the whole name, the partial name, and the extension.
Was This Post Helpful? 1
  • +
  • -

#7 persson121   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 30-June 12

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 04:15 PM

View PostCurtis Rutland, on 11 July 2012 - 04:08 PM, said:

Personally, I prefer the DirectoryInfo and FileInfo objects. They return more useful objects than just lists of strings. In this case, if you use DirectoryInfo to search for these files, it returns an array of FileInfos, which have properties that have the whole name, the partial name, and the extension.


How do you mean?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,461
  • Joined: 05-May 12

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 04:25 PM

View Postpersson121, on 11 July 2012 - 04:15 PM, said:

View PostCurtis Rutland, on 11 July 2012 - 04:08 PM, said:

Personally, I prefer the DirectoryInfo and FileInfo objects. They return more useful objects than just lists of strings. In this case, if you use DirectoryInfo to search for these files, it returns an array of FileInfos, which have properties that have the whole name, the partial name, and the extension.


How do you mean?


Instead of using Directory.GetFiles() which returns strings, using DirectoryInfo.GetFiles() which returns FileInfo's.

Also, the wild card you were searching for in your latest code was just "*.ini", I thought you were looking for "bookmark_*.ini" ?

This post has been edited by Skydiver: 11 July 2012 - 04:26 PM

Was This Post Helpful? 1
  • +
  • -

#9 persson121   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 30-June 12

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 04:45 PM

View PostSkydiver, on 11 July 2012 - 04:25 PM, said:

View Postpersson121, on 11 July 2012 - 04:15 PM, said:

View PostCurtis Rutland, on 11 July 2012 - 04:08 PM, said:

Personally, I prefer the DirectoryInfo and FileInfo objects. They return more useful objects than just lists of strings. In this case, if you use DirectoryInfo to search for these files, it returns an array of FileInfos, which have properties that have the whole name, the partial name, and the extension.


How do you mean?


Instead of using Directory.GetFiles() which returns strings, using DirectoryInfo.GetFiles() which returns FileInfo's.

Also, the wild card you were searching for in your latest code was just "*.ini", I thought you were looking for "bookmark_*.ini" ?


Done! :donatello:
Im a python coder and i allways take the easy way :)
// Read all the names and put them into a list
            string[] filenames = Directory.GetFiles(@"C:\bookmarks\", "*.ini", SearchOption.TopDirectoryOnly);

            List<string> lines_new = new List<string>(filenames);
            // Count the files.
            int fileCount = filenames.Length;

            // The listBox will now read.
            for (int i = 0; i < fileCount; i++)
            {
                // the string random is a string to remove.
                string random = lines[i].Replace("C:\bookmarks\n", "");
                random = random.Replace(".ini", "");
                if (listBox.Items.Contains(random))
                {
                    // Do nothing
                }
                else
                {
                    listBox.Items.Add(random);
                }


            }


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,461
  • Joined: 05-May 12

Re: How do i get all the files names who are in the folder?

Posted 11 July 2012 - 05:27 PM

That code is not the easy way. It copies the array of strings into a list and then does nothing with it. It keeps stripping away a leading directory prefix, and a trailing extension by doing String.Replace() when String.SubString() would be more efficient.

This is slightly easier:
var dirInfo = new DirectoryInfo(@"C:\bookmarks");
foreach(var fileInfo in dirInfo.EnumerateFiles("*.ini"))
{
    string fileName = Path.GetFileNameWithoutExtension(fileInfo.Name);
    if (!listBox.Items.Contains(fileName))
        listBox.Items.Add(fileName);
}



Of course, the code assumes that there was a typo in the original line:
string random = lines[i].Replace("C:\bookmarks\n", "");


This post has been edited by Skydiver: 11 July 2012 - 05:31 PM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1