Now for this article we will create a new asp.net core mvc application and create a folder in wwwroot folder and add sone files on it as shown below.
So, in this article we will provide a text field where user will enter the file name and user entered file name we are going to capture from view to controller and we will validate is file exists or not. If file exists, we will delete the file from wwwroot folder and display a user-friendly message to user. Is file does not exist we will display message file does not exist in wwwroot folder.
Now we will add a model class file and add the property as shown below.
public class FileModel
{
public string FileName { get; set; }
public List<FileModel> Files { get; set; }
}
In above mode class file i have declared 2 properties one for file name and second one is the list of all the files. We will user List to capture all the files and display all the file on page load available in wwwroot folder. Now let's add a controller class file and add the HttpGet method. In this IActionResult we will add the below code.
[HttpGet]
public IActionResult Index()
{
string parentPath = @"wwwroot\DemoFiles\";
string finalPath = Path.Combine(Directory.GetCurrentDirectory(), parentPath);
FileModel fileModel = new FileModel();
if (Path.Exists(finalPath))
{
fileModel.Files = new List<FileModel>();
var data = Directory.GetFiles(finalPath);
foreach (var item in data)
{
fileModel.Files.Add(new FileModel { FileName = Path.GetFileName(item) });
}
}
else
{
ViewBag.Message = "Given path does not exists.";
}
return View(fileModel);
}
In above code I have taken a variable for wwwroot folder path and after that i have used Path.Combine to prepare the upload path by concatenating path string with Directory.GetCurrentDirectory() and the folder path.After preparing the path I have validated whether the file exists or not. if it exists, I have created object for file list. After that I have used Directory.GetFiles to read the file form the given path. After getting all the files from folder i have used for each loop to prepare the list of all the files available in the folder. Now let's create the view and add the below code.
@model FileModel
@{
ViewData["Title"] = "Home Page";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<div style="color:red">@ViewBag.Message</div>
<div style="font-weight:bold;">Enter File Name With Extention:</div>
<div>
@Html.TextBoxFor(m => m.FileName, new { @class = "form-control" })
</div>
<br />
<div><input type="submit" value="Delete File" /></div>
<br />
<h2>File List In wwwwroot Folder</h2>
<br />
<ul>
@foreach (var item in Model.Files)
{
<li>@item.FileName</li>
}
</ul>
}
In above code i have add the reference of the model class file. Now here i have added form tag with the help of Html.BeginForm. In this tag i have added the controller and the IActionResult method name to post the value to receive the detail.After adding the form tag i have TextBoxfor control to get the file name which we want to delete. At bottom I have displayed list of all the files which are present in the wwwroot folder. Now let's tun the code and check the current output.
[HttpPost]
public IActionResult Index(FileModel file)
{
string parentPath = @"wwwroot\DemoFiles\";
FileModel fileModel = new FileModel();
fileModel.Files = new List<FileModel>();
try
{
string folderfilepath = Path.Combine(Directory.GetCurrentDirectory(), parentPath);
string deletefilepath = Path.Combine(Directory.GetCurrentDirectory(), parentPath, file.FileName);
if (Path.Exists(deletefilepath))
{
System.IO.File.Delete(deletefilepath);
ViewBag.Message = "File deleted successfully.";
}
else
{
ViewBag.Message = "File does not exists.";
}
//Get the file list after delete
var data = Directory.GetFiles(folderfilepath);
foreach (var item in data)
{
fileModel.Files.Add(new FileModel { FileName = Path.GetFileName(item) });
}
}
catch (Exception ex)
{
ViewBag.Message = ex.Message;
}
return View(fileModel);
}
In above code i have passed the FileModel as a parameter to the post method. This will help us to get value form the view to controller. In next line i have defined a variable and created the object for FileModel class. Here one thing we need to take care the variable named as folderfilepath and deletefilepath. In these two variables I have stored the folder path and the file path. Here folder path I have used for reading all the remaining file in wwwroot folder and deletefilepath ios for deleting the file.After this I am validating whether the file exists or not. If file does not exist, we will display a friendly message and if file exists System.IO.File.Delete(deletefilepath); to delete the file. After deleting the file, I have written the code to again get all the file from the wwwroot folder. Now let's run the code and check the output.
Now let's add a file with proper name and click on delete file button.
Now let's check the wwwroot folder.