Rexer Raj
Rexer Raj

Reputation: 21

In azure web app File.Exits return false actually file exists

We have a mail template in ~/Content/EmailTemplate/template.cshtml. But whenever we do the following:

var path="D:/site/wwwroot/Content/EmailTemplate/template.cshtml"
File.Exist(path)

File.Exist(path) returns false. While debugging in local source it works fine. It returns false in azure web app only. I have checked the file already exists there.

Upvotes: 2

Views: 675

Answers (2)

Tom Sun
Tom Sun

Reputation: 24549

For Azure WebApp, D:\home is shared for us and we could read or write file in this path. More detail about Home directory access please refer to the WebApp Sandbox. File structure on Azure please refer to another document. We could browse it from Kudu (http://yourwebsite.scm.azurewebsites.net/) tool. In your case, we also could use the following code in the Azure WebApp.

string path = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot\Content\EmailTemplate\template.cshtml"

Upvotes: 0

user2411670
user2411670

Reputation:

If you move the above code to another environment it will break.

I would suggest that you use map path as this will assure that you can move from hosting environment to hosting environment, including your local development environment.

string path = Server.MapPath("~/path/tofile");

You will also have higher confidence that you are targeting the file correctly.

Upvotes: 2

Related Questions