Reputation: 80
How to create a folder in wwwroot by coding? im use asp core 2.2 this code not working: Path.Combine(_hostingEnvironment.WebRootPath, "wwwroot\UploadFile");a
Upvotes: 2
Views: 4500
Reputation: 94
Path.Combine
will only give you the path string. It won't create a directory by itself.
I suggest passing IHostingEnvironment
to your class through dependency injection and then using WebRootPath
to get the path for wwwroot folder (i.e. not hardcoding it).
public class YourClass
{
private readonly IHostingEnvironment _env;
public YourClass(IHostingEnvironment env)
{
_env = env;
}
public YourMethod()
{
string path = Path.Combine(_env.WebRootPath, "UploadFile").ToLower();
//if path does not exist -> create it
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
}
}
Upvotes: 1
Reputation: 27548
Please try with below code sample :
var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\UploadFile");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
Upvotes: 5