1

I have a simple .Net Core MVC web application that I deploy to Azure. In the application, I am creating a little text file called "test.txt" using File.CreateText(). This works fine on my local PC, but when I deploy it to Azure, I get a strange message: "Could not find file 'D:\home\site\wwwroot\wwwroot\test.txt'."

Indeed, the file does not exist--that's why I'm creating it. Also, it appears someone else on SO is having a similar problem: FileNotFoundException when using System.IO.Directory.CreateDirectory()

Do I not have write permissions? Why is Azure not letting me create the file?

Code (in Startup.cs):

public void Configure(IApplicationBuilder app, IHostingEnvironment env){
    using (var sw = File.CreateText(env.WebRootPath + "/test.txt")) { }
}

Screenshot of error:

enter image description here

user3163495
  • 2,425
  • 2
  • 26
  • 43
  • Is there any content in `using (var sw = File.CreateText(env.WebRootPath + "/test.txt"))`? I made a test with your code, it works correct when I deploy to azure web app service. – Edward Jul 24 '19 at 09:42
  • @TaoZhou there's nothing in the "using" block. i'm just using it to create the file. also, it looks like someone is having a similar problem: https://stackoverflow.com/questions/54552147/filenotfoundexception-when-using-system-io-directory-createdirectory – user3163495 Jul 24 '19 at 17:35
  • How did you publish .net core to azure? – Edward Jul 25 '19 at 01:13
  • Did you solve this problem? I'm having the same problem. – Fatih GÜRDAL Dec 08 '19 at 15:13
  • 1
    @FatihGÜRDAL I just posted the answer to this question, see below – user3163495 Dec 08 '19 at 18:48

2 Answers2

2

I found the reason for this error shortly after posting this question, but forgot to post the answer:

It turns out that when you deploy to Azure, your site runs from a temporary directory, which gets wiped and re-created every time you deploy. Because of this, Azure disables the creation and editing of files in the app's directory, since they're just going to get wiped upon your next deployment (it would be nice if the error message Azure displayed was a little more informative).

Therefore, the way to create and edit files on your Azure app and have them persist across deployments is to use one of the following:

  1. Database BLOB storage (simply store your files as byte arrays in a database. doesn't cost any money if you are already using a database)
  2. Azure BLOB storage (store your files to a special cloud on Azure. costs money)

I read this on some site shortly after posting this question but I can't remember where, so I apologize for not having the source.

user3163495
  • 2,425
  • 2
  • 26
  • 43
0

The best way is use the blog storage AZURE to do this. But you can try save in the root this way:

static void Main(string[] args)
{
    // Create a string with a line of text.
    string text = "First line" + Environment.NewLine;

    // Set a variable to the Documents path.
    string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    // Write the text to a new file named "WriteFile.txt".
    File.WriteAllText(Path.Combine(docPath, "WriteFile.txt"), text);

    // Create a string array with the additional lines of text
    string[] lines = { "New line 1", "New line 2" };

    // Append new lines of text to the file
    File.AppendAllLines(Path.Combine(docPath, "WriteFile.txt"), lines);
}

Reference: https://learn.microsoft.com/pt-br/dotnet/standard/io/how-to-write-text-to-a-file