create a file with C# content and save to a folder on the computer

Asked

Viewed 8,291 times

2

I need to create a report and save this report in a folder inside the C disk: from the computer, I’m only able to save the file without the contents and with the name blank.

The code to save:

string folder = System.Configuration.ConfigurationManager.AppSettings["Relatorios"] + usuario;
string arquivo = folder + @"\" + nomearquivo;

if (!Directory.Exists(folder))
{
    Directory.CreateDirectory(arquivo + ".txt " + relatorio);
}

if (!File.Exists(arquivo))
{
    File.Create(arquivo+".txt " + relatorio);
}               
else
{
    File.Delete(arquivo);
    File.Create(arquivo + ".txt " + relatorio);
}
  • 1

    Can be an example with any content?

  • may be yes...

  • Douglas, did any of the answers solve your problem?

  • It was, I just can’t save with the report

  • Ok, so you can mark the answer that solved your problem as correct using the on her left side, right? About the new problem, you can open a new question at any time if you are in need of help.

  • very good, the problem is that the file doesn’t update as you add characters

Show 1 more comment

2 answers

8


There are several problems there. I improved your code in a general.

  1. Use Path.Combine() instead of concatenating the strings with the backslash (this is more of a hint).

  2. No need to check if the directory exists before Directory.CreateDirectory() why this method only creates the file if it does not exist.

  3. It is not necessary to delete the file to recreate it. The method File.Create() creates a file if it does not exist, if the file with this name already existed, it will be overwritten.

  4. Beware of streams open.

    File.Create() will return a FileStream, this FileStream is not being closed and you need to be careful with this. That’s why I put Dispose() after the Create().

    Read about this at: What is the usefulness of using?, What types of resources are released in a "using statement"?, I should always use Dispose?

  5. Failed to write the contents of the file. The method Create() receives only the file name, you are concatenating what should be the content along with the name.

    In my example, I used WriteAllLines() to write it. It takes as parameter a IEnumerable<string> and makes every element of this collection a file line. I used this method because it is much simpler and does not run the risk of resource leakage.


The code:

Supposing relatorio be a collection of string (array, list, etc.).

List<string> relatorio = /*Fazer algo para carregar o relatório*/;

string folder = System.Configuration.ConfigurationManager.AppSettings["Relatorios"] + usuario;;
string arquivo = Path.Combine(folder, "arquivo.txt");
        
Directory.CreateDirectory(folder);

File.Create(arquivo).Dispose();

File.WriteAllLines(arquivo, relatorio);
  • your answer helped me in a doubt of my own, however this way the file is created inside the server, would have as create in the local machine of the client ?

  • @Desalex In a web application it is impossible. What you can do is generate a file on the server and return it to the client for the user to download.

  • like this guy did in this example of this video ai -> https://www.youtube.com/watch?v=NJRiGGgDEpE I need this, "download the created file" and I’m not getting it, I did it and mine does not download anything, nothing happens.

  • @Desalex Open a question then, man.

  • already open, is that ngm answered, but I really need it :(

  • @Desalex I searched your profile and did not find, if you send the link I try to answer...

Show 1 more comment

2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.