1

I am trying to obtain a file of name "fileName.wav" from the directory whose path has been specified im not able to get the desired function .. any help in this regard is appreciated.

static void Main(string[] args)
{
    string[] fileEntries = Directory.GetFiles(
        @"D:\project\Benten_lat\BentenPj_000_20141124_final\Testing\DPCPlus\Ref\Generated_Ref_Outputs_MSVS", "*.wav");

    foreach (string fileName in fileEntries)
    {
        string[] fileEntries1 = Directory.GetFiles(
            @"D:\project\older versions\dpc_latest\Testing\DPCPlus\input");

        foreach (string  fileName1 in fileEntries1)
        {
            Console.WriteLine(Path.GetFileName(fileName1));
        }
    }
}
5
  • 8
    I don't quite understand what you're trying to achieve. Your code reminds me for if anit-pattern Dec 30, 2014 at 7:20
  • Getting files from path that is not direcotry is not good idea...
    – user2160375
    Dec 30, 2014 at 7:21
  • You know the directory, you know the file name. Why not just read/play it directly?
    – danish
    Dec 30, 2014 at 7:23
  • 3
    string[] fileEntries1 = Directory.GetFiles(@"D:\project\Benten_lat\BentenPj_000_20141124_final\Testing\DPCPlus\Ref\Generated_Ref_Outputs_MSVS\filename.wav"); what are you doing here. Getting files from file?
    – dotnetstep
    Dec 30, 2014 at 7:25
  • Define the use of the word obtain
    – TheGeneral
    Dec 30, 2014 at 7:28

3 Answers 3

1

This Code will search for all .xml extension files with matches abCd name from C:\\SomeDir\\ folder.

string cCId ="abCd";
DirectoryInfo di = new DirectoryInfo("C:\\SomeDir\\");
FileInfo[] orderFiles = di.GetFiles("*" + cCId + "*.xml", SearchOption.TopDirectoryOnly);

You should change this code according to your needs. Hope this helps you.

1

If you want to extract name and extension from a file-path, you can use following code:

string result = Path.GetFileName(yourFilePath); // result will be filename.wav

You can not use GetFiles method for a file path!

Directory.GetFiles(@"D:\project\Benten_lat\BentenPj_000_20141124_final\Testing\DPCPlus\Ref\Generated_Ref_Outputs_MSVS\filename.wav") //Error

The input for this method should be a directory!

0

Assumptions based on your question (please correct me if I am wrong):

  • You know the folder (e.g. the user entered it)
  • You know the file name (e.g. it is hard coded because it must be the same always)

In this case you do not really need to search the file.

Solution:

string folderPath = @"D:\somewhere";
string fileName = "file.wav";
string filePath = Path.Combine(folderPath, fileName);
if (File.Exists(filePath))
{
    // continue reading the file, etc.
}
else
{
    // handle file not found
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.