Link to home
Create AccountLog in
Avatar of Fatehbourghoud
Fatehbourghoud

asked on

System.IO.Directory.GetFiles

HI Expert ;

My C# Code have to get the files with pattern  
"SAAA502" + DateTime.Now.ToString("yyyyMMdd") + "_*.CSV"  witch is the file name , I coded that as following but  dont work !!!

following My Code :  

 string fileStore502 = string.Empty;

 string[] filesStore502 = System.IO.Directory.GetFiles(directoryPath, "SAAA502" + DateTime.Now.ToString("yyyyMMdd") + "_*.CSV");

so when I test My code with file SAAA50220101230_231948.CSV My string Array filesStore502 steel empty!!! and the methode didn't get the file using the pattern above

Can you please let me know how to fix that

Thank s
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Your "wildcard" expression here

"_*.CSV"

is causing you the problems.
ASKER CERTIFIED SOLUTION
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
@nepaluz,
Where did you get that from? It is a valid expression.

@Fateh,
Have you confirmed that the variable "directoryPath" has the path you expect and is properly formatted?

- Anthony
Avatar of Dmitry G
This wildcard is not causing any problems at my computer.

I have a file named: "SAAA50220101231_231948.csv".
I vae the code below. And everything works...
private void button2_Click(object sender, EventArgs e)
        {
            string directoryPath = @"C:\work";
            string[] filesStore502 = System.IO.Directory.GetFiles( directoryPath,  "SAAA502" + DateTime.Now.ToString("yyyyMMdd") + "_*.CSV");
            foreach (string filename in filesStore502)
            {
                System.Diagnostics.Debug.WriteLine(filename);
            }

        }

Open in new window

Exactly my point anarki. Thanks.

My only guess, since there are no errors, is the directoryPath variable.

- Anthony
If path does not exist  - an exception is thrown... But yes, if it's wrong (and still exist) - nothing will be found.
Avatar of Fatehbourghoud
Fatehbourghoud

ASKER

Thanks nepaluz
@ anarki
@AlaRiva

The nepaluz solution works for me
You are welcome. Glad to help.