MC Fer MC Fer - 1 month ago 6
C# Question

Error saving a file on server

I am generating a Key and i need to record on server. When i tried locally, it Works ! The problem is when i try on the server...
The archive is generated but when ON THE SERVER the download finish,but i cannot open and cannot save. I donĀ“t get an error...but i just cannot save...

The code:

string fileNamePrivate = @"pasta1/archive.txt";
Directory.CreateDirectory(Path.GetDirectoryName(fileNamePrivate));
StreamWriter textWriterPriv = new StreamWriter(fileNamePrivate, true);
var pemWriter = new PemWriter(textWriterPriv);
pemWriter.WriteObject(keyPair.Private);
pemWriter.Writer.Flush();
textWriterPriv.Close();

Answer

Try to use

string fileNamePrivate = "~/PASTA1/archive.txt";
Directory.CreateDirectory(Path.GetDirectoryName(HostingEnvironment.MapPath(fileNamePrivate)));
StreamWriter textWriterPriv = new StreamWriter(fileNamePrivate, false);

How is a Server you have to use HostingEnvironment.MapPath and set "false" instead "true" on StreamWriter if you need to overwrite.