在添加(创建)或修改特定文件夹时获取文件元数据信息
本文关键字:文件夹 获取 文件 信息 元数据 添加 创建 修改 | 更新日期: 2023-09-27 18:09:52
我想获得在特定文件夹中添加或修改文件时的文件信息。我尝试在c#中使用文件监视器,但它不能正常工作。如果有别的办法,请告诉我。我希望在c#或Java中编写此内容。这是我的代码。
private void fswBarCode_Changed(object sender, FileSystemEventArgs e)
{
try
{
RequestsManager objRequestsManager = new RequestsManager();
Requests objRequests = new Requests();
DataTable dt = (DataTable)objRequestsManager.GetPendingRequests();
List<Requests> lstRequest = new List<Requests>();
foreach (DataRow dr in dt.Rows)
{
objRequests = new Requests();
objRequests.RecipeID = Convert.ToInt32(dr["intRecipeID"]);
objRequests.ChemicalReqID = Convert.ToInt32(dr["intCRHeaderID"]);
lstRequest.Add(objRequests);
}
if (e.ChangeType == System.IO.WatcherChangeTypes.Changed)
{
FileInfo file = new FileInfo(e.FullPath);
string fileName = file.Name;
char[] ch = { '.' };
string strrecipeID = fileName.Split(ch)[0];
int intRecipeID = Convert.ToInt32(strrecipeID);
if (lstRequest.Count() != 0)
{
lstRequest = (from lst in lstRequest
where lst.RecipeID.Equals(intRecipeID)
select lst).ToList();
if (lstRequest.Count() != 0)
{
bool blIsSuccess = objRequestsManager.SaveChemicalRequisitions(lstRequest);
if (blIsSuccess == true)
{
Logger.LoggIssueItems(strrecipeID);
List<Requests> lstRequest1 = (List<Requests>)PendingRequests();
}
}
}
}
}
catch (Exception ex)
{
Logger.LoggError(ex, "fswBarCode_Changed");
}
}
您可以通过使用Java nio api来实现这一点,如下所示
java.nio.file包及其相关包;java.nio.file。属性,为文件I/O提供全面的支持以及访问默认文件系统
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());