views:

51

answers:

4

We have a network folder that is the landing place for csv files processed by different servers. We alter the extension of the csv files to match the server that processed the file; for instance a file with the name FooBar that was process by Server1 would land in the network share with the name FooBar.Server_1.

The information I have available to to me is file name: FooBar and the path to the share. I am not guaranteed the extension as there are multiple servers sending csv files to the network share. I am guaranteed that there will only be one FooBar file in the share.

Is there a method in .net 2.0 that can be used to get the extension of the csv armed only with the path and filename? Or do I need to craft my own?

+2  A: 

Directory.GetFiles(NETWORK_FOLDER_PATH, "FooBar.*")[0] will give you the full path.

Path.GetExtension will give you the extension.

If you want to put it all together:

string extension = Path.GetExtension(
    Directory.GetFiles(NETWORK_FOLDER_PATH, "FooBar.*")[0]);
Daniel Straight
I assumed he only cared about the extension in that he didn't know what it was. My bad.
Mike Caron
+2  A: 

This should do the trick:

string[] results = System.IO.Directory.GetFiles("\\\\sharepath\\here", "FooBar.*", System.IO.SearchOption.AllDirectories);

if(results.Length > 0) {
    //found it
    DoSomethingWith(results[0]);
} else {
    // :(
}
Mike Caron
+1  A: 

If you know the directory where the file will reside you can use DirectoryInfo.GetFiles("SearchPattern"): DirectoryInfo.GetFiles("FooBar.*")

Ando
+1  A: 
DirectoryInfo di = new DirectoryInfo(yourPath)
{
  FileInfo[] files = di.GetFiles(FooBar.*);
}
AllenG