This project is read-only.

Determining if a content file exists

Topics: Writing themes
May 26, 2011 at 3:17 AM

I have a custom theme and I'm trying to determine if a particular file exists in the content directory for that theme. I've tried using the following:

if (File.Exists("~/Themes/MyTheme/Content/MyImage.png")) { // do something }

But that always seems to return false. I've tried using Url.Content to obtain the absolute path, but that returns false as well. I know the RazorViewEngine has a FileExists method that expects a controller and virtualPath, but I can't seem to get that working either.

Any ideas?

May 26, 2011 at 7:09 PM

File.Exists is from System.IO, it knows nothing about virtual paths. You need to resolve it to a local physical path using MapPath.

May 26, 2011 at 7:40 PM

Or inject IVirtualPathProvider which has FileExists and TryFileExists methods for virtual paths.

You can also use ISiteThemeService to discover the location of the current theme.

May 26, 2011 at 7:41 PM

Yes, what he said, forget my answer.