'IFileProvider returning 01/01/0001 for last modified date of all files in Physical Provider
In startup I have set my IFileProvider in configure services as follows:
IFileProvider physicalProvider = new PhysicalFileProvider(Directory.GetParent(env.ContentRootPath).ToString());
services.AddSingleton<IFileProvider>(physicalProvider);
Now when I go to use to find the files last modified date in my controller I get date 01/01/0001 for all files. My code is as follows:
private readonly IWebHostEnvironment env;
private IFileProvider _fileProvider;
public IConfiguration Configuration { get; }
public MainReviewController(LoginDBContext context, IConfiguration configuration, IWebHostEnvironment env, IFileProvider fileProvider)
{
_context = context;
connectionString = configuration["ConnectionStrings:DBContext"];
this.env = env;
_fileProvider = fileProvider;
}
After I use dependency injection to use the file provider I use the following code in my post request
var rootPath = Path.Combine(Directory.GetParent(env.ContentRootPath).ToString(), "ARMSFiles");
foreach (var file in mainreviewdocuments)
{
var filePath = Path.Combine(rootPath, file.DOCUMENT_FULL_PATH);
System.Diagnostics.Debug.WriteLine(filePath);
System.Diagnostics.Debug.WriteLine(_fileProvider.GetFileInfo(filePath).LastModified);
}
Any ideas on this? In my startup i use dependency injection as well to ensure i am using the same env variable as my controller so the directories should line up. It is basically a folder outside of my root folder since that is a bit more secure and I use it for a webdav when I actually deploy this.
Solution 1:[1]
I use below code, and get the value of LastModified;
IDirectoryContents dirContents = _fileProvider.GetDirectoryContents("ARMSFiles");
IFileInfo fileInfo = _fileProvider.GetFileInfo("ARMSFiles/TextFile.txt");
Test Result
Test Code:
Tips: below code just for test, pls don't use it in your production environment directly.
1. Test Method
public IActionResult TestFileProvider()
{
// the static root path, just for test
string filePath = @"D:\M***e\F***e\TestCookie\ARMSFiles\TextFile.txt";
IDirectoryContents dirContents = _fileProvider.GetDirectoryContents("ARMSFiles");
IFileInfo fileInfo = _fileProvider.GetFileInfo("ARMSFiles/TextFile.txt");
System.Diagnostics.Debug.WriteLine(filePath);
System.Diagnostics.Debug.WriteLine(fileInfo.LastModified);
return Ok(fileInfo.LastModified);
}
2. ConfigureServices Method
public void ConfigureServices(IServiceCollection services)
{
//IFileProvider physicalProvider = new PhysicalFileProvider(Directory.GetParent(env.ContentRootPath).ToString());
var physicalProvider = _env.ContentRootFileProvider;
var embeddedProvider = new EmbeddedFileProvider(Assembly.GetEntryAssembly());
var compositeProvider = new CompositeFileProvider(physicalProvider, embeddedProvider);
services.AddSingleton<IFileProvider>(compositeProvider);
...
services.AddControllersWithViews();
}
3. Configure Method
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "ARMSFiles")),
RequestPath = "/ARMSFiles"
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


