在特定文件夹中添加(创建)或修改文件元数据信息时获取文件元数据信息

时间:2016-10-03 03:10:28

标签: java c# file

我想在特定文件夹中添加或修改文件时获取文件信息。我尝试在C#中使用File Watcher,但它无法正常工作。如果有其他方法,请告诉我。我希望用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");

    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用Java nio api实现此目的,如下所示

  

java.nio.file包及其相关包,   java.nio.file.attribute,为文件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());