How to search for a .jpg/PDF with just the file name (Search "dog.jpg" with just "dog") Can it be done? C#

StackOverflow https://stackoverflow.com/questions/16839872

質問

Is there a way to search for a jpg or PDF if you just have the name of the file?

Dummy site: www.dummysite1.com/animal=dog

What I want to do is parse the URL, take the parsed name "dog" and search through a folder for a .jpg of the same name.

So lets say I have a folder named "imgAnimal" after I parse "dog" I want to look in "imgAnimal" folder for "dog.jpg". I then want to open "dog.jpg" in another window.

I don't know if this is the best way to even do this or if its even possible, but just thinking of a way and not being familiar with C# it's what I came up with. The folder will have a lot of images in it so I don't know if searching through them all would be the best thing or if it would matter.

Any ideas are welcomed I don't HAVE to do it this way if there is a better way. Thanks.

Edit:

I have the parsed code and everything having to do with pulling the URL part written. The only thing I'm really asking is - Is there a way in C# to take a word like "dog" and use it in a search to find "dog.jpg" then open that jpg in a new web window?

役に立ちましたか?

解決

You can use the System.DirectoryInfo.GetFiles method to find the file:

string param = GetParamFromQueryString();
string relativeWebServerPathToFolder = "../images/imgAnimal";

var dirInfo = new DirectoryInfo(Server.MapPath(relativeWebServerPathToFolder));

var foundFiles = dirInfo.GetFiles(param + ".jpg"); // you can also use + ".*" to search for all files

if (foundFiles.Length == 1)
{
    ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + relativeWebServerPathToFolder + foundFiles.Name + "');", true); // registers a script block to pop open the file
}

Docs for ClientScript.RegisterStartupScript: http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top