Error saving file in different path to the root project C #

1

I have an application in C # that downloads certain files from an API using RestSharp, I do not have problems when downloading them in the root of the project, but if for example I try to download them in the path C: / ProjectName / file.txt I have an error of this type:

Excepción no controlada del tipo 'System.IO.DirectoryNotFoundException' en 
mscorlib.dll
No se puede encontrar una parte de la ruta de acceso 'C:\Directorio\name.txt'.

When I tell him to save in this way he gives me an error:

client.DownloadData(request).SaveAs(@"C:/Directorio/"+id+"-name" + ".txt");

But when I leave it without a path, it saves well in the root directory:

client.DownloadData(request).SaveAs(id + "-name" + ".txt");

How should I tell you to save in C or My Documents?

    
asked by DVertel 11.09.2018 в 22:18
source

1 answer

1

After your comments, the problem is simply that the directory did not exist. SaveAs expects a route that already exists, in no case will create it.

You can create it by doing something like the following:

string carpeta =@"C:\Directorio"; 
if(!System.IO.Directory.Exists(carpeta))          //Si no existe ya la carpeta
    System.IO.Directory.CreateDirectory(carpeta); //la creamos
    
answered by 12.09.2018 / 16:45
source