开发者

Can I find a file by filename only (i.e. extension unknown)

开发者 https://www.devze.com 2023-01-08 22:06 出处:网络
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

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 s开发者_Go百科ending 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?


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]);


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 {
    // :(
}


DirectoryInfo di = new DirectoryInfo(yourPath)
{
  FileInfo[] files = di.GetFiles(FooBar.*);
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消