Is it possible to create a file outside the project folder?

A yes/no question basically. This is the code I currently use to create and store files. When the StoragePath is "uploads", it creates an upload directory within my project folder.
c#
var savePath = Path.Combine(StoragePath, "test.png");
string directory = Path.GetDirectoryName(savePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

using (var fileStream = new FileStream(savePath, FileMode.Create))
{
await myObject.Image.CopyToAsync(fileStream);
}
c#
var savePath = Path.Combine(StoragePath, "test.png");
string directory = Path.GetDirectoryName(savePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

using (var fileStream = new FileStream(savePath, FileMode.Create))
{
await myObject.Image.CopyToAsync(fileStream);
}
5 Replies
Angius
Angius•2w ago
Yes
kurumi
kurumi•2w ago
Yes
electronic heartbreak.
electronic heartbreak.OP•2w ago
Damnit. I tried to use @"D:\UploadedFiles"; now as StoragePath but it does not create the directory and it does not create the file. I think this issue arises due to my use of Docker 😓 .
Pobiega
Pobiega•2w ago
correct. if this runs inside a container, there is no "D:" drive at all the container is like a lightweight linux VM you'd need to map a volume on your host to the container, then access it with the container path
electronic heartbreak.
electronic heartbreak.OP•2w ago
That's indeed the solution! I almost gave up last night out of frustration. Gonna work on this anytime soon so thank you for your reply.
Want results from more Discord servers?
Add your server