How can I save TXT file in C: \ directory using C #?

3

I'm trying to make a small program for scrolling data and I want it to save a .txt file that stores the scrolls so it can be read later. The program is practically whole ready, all the features working, but when I try to select the directory where .txt will be saved as C:\ it will not let me use, probably because of permission problems.

As I'm going to make an installer of this program to distribute it on the internet, I need to save it to a directory that is standard on any PC, so I thought C:\ would be ideal. Could you give me a hand?

    
asked by anonymous 30.10.2015 / 13:17

2 answers

1

If you are trying to save to a folder inside C it might not exist, and that is the case, you can check if it exists and create it if it is the case:

var dirPath = @"C:\Temp\";
var filePath = dirPath + "arquivo.extensao";

// Verifica se o diretório não existe e cria 
if (!Directory.Exists(dirPath))
    Directory.CreateDirectory(dirPath);

// Da mesma maneira para o arquivo
if (!File.Exists(filePath))
    using (File.Create(filePath)) { }
    
30.10.2015 / 13:36
1

System.IO.Path.GetTempPath() is just a wrapper for a native call to GetTempPath (..) in Kernel32.

Take a look at link

    
30.10.2015 / 13:31