Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have several files in folder ,i want to see all extract there name by code
i use
n=Directory.GetFiles(CurrentDirectory).Length(CurrentDir);
then i used a for loop to see their names ,i have seen some names but not all

What I have tried:

fyles = Directory.GetFiles(CurrentDirectory);
n=Directory.GetFiles(CurrentDirectory).Length(CurrentDir);
for(i=0;i<n;i++)
MessageBox.Show(fyles[i]);
Posted
Updated 6-Dec-23 0:54am

First problem: don't call GetFiles twice. If you want the number of files that were returned, look at the Length property on the array you've already stored.

Second problem: the Length property doesn't have any arguments. Which means your call to .Length(CurrentDir) must be calling an extension method that you've created. Since you haven't shown us that extension method, we can't tell you why it's not returning the correct value.

Third problem: nobody here has access to your disk. We cannot guess why you are not seeing all of the files you expect to see.

Try using:
C#
string[] files = Directory.GetFiles(CurrentDirectory);
for (int i = 0; i < files.Length; i++)
{
    MessageBox.Show(files[i]);
}
or:
C#
string[] files = Directory.GetFiles(CurrentDirectory);
foreach (string file in files)
{
    MessageBox.Show(file);
}
or even:
C#
foreach (string file in Directory.EnumerateFiles(CurrentDirectory))
{
    MessageBox.Show(file);
}
 
Share this answer
 
Comments
Maciej Los 6-Dec-23 7:05am    
Short and to the point!
Engineer khalid 6-Dec-23 8:17am    
i need to store the file names to an array and send them to a text file like this

sw.WriteLine(file[i]); // <=== it did not works
Richard Deeming 6-Dec-23 8:20am    
And what, precisely, does "it did not work" mean?

Remember, we cannot see your screen, access your computer, or read your mind. All we have to go on is what you tell us. Comments like "did not work" tell us precisely nothing!
Dave Kreskowiak 6-Dec-23 10:01am    
NM. Not enough caffeine today. You have to show the error message you're getting, if any, or otherwise describe what's going on.

Where are you trying to write this file and what is the code that is setting that up?
Engineer khalid 7-Dec-23 1:16am    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;

namespace testGetFile
{
public partial class Form1 : Form
{
FileStream fs, fg;
StreamWriter sw;

protected override void OnClosing(CancelEventArgs e)
{
if (sw != null) sw.Close(); if (fg != null) fg.Close();
}
//--------------------------------------------------------
public Form1()
{
InitializeComponent();
}
//------------------------------------------------------
private void btnStartClick(object sender, EventArgs e)
{
int i, Nfile = 0;
string CurrentDirectory = "";
string[] iFile = new string[50];
for (i = 0; i < 50; i++) iFile[i] = "";
CurrentDirectory = Directory.GetCurrentDirectory();
//**********************************************************
fg = new FileStream("ListOfFileName.txt", FileMode.Truncate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fg);
//----------------------------------------------------------
string[] files = Directory.GetFiles(CurrentDirectory);

i = 0;
Nfile = 0;
foreach (string file in files)
{
MessageBox.Show(file);
Nfile++;
}
for (i = 0; i < Nfile; i++)
{
iFile[i] = files[i];
sw.WriteLine(iFile[i]);
}
}//btnStartClick

}
}
Quote:
i have seen some names but not all


Please, refer to the MSDN documentation: Directory.GetFiles Method (System.IO) | Microsoft Learn[^]

As you can see, there is a few overloaded methods. Maybe you need to use that one which uses EnumerationOptions Class (System.IO) | Microsoft Learn[^].

Quote:
The attributes to skip. The default is FileAttributes.Hidden | FileAttributes.System.

I do believe you want to skip none :)

On the other hand, i'd suggest to read this: Directory.Get.Files search pattern problem[^]


Tip: You can use FileInfo class[^] to get more information about particular file, such as (properties):
Attributes
CreationTime
CreationTimeUtc
Directory
DirectoryName
Exists
Extension
FullName
IsReadOnly
LastAccessTime
LastAccessTimeUtc
LastWriteTime
LastWriteTimeUtc
Length
LinkTarget
Name
UnixFileMode
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900