/// <summary> /// 设置文件扩展属性 /// </summary> /// <param name="fileName"></param> /// <param name="dicKeyValue"></param> /// <returns></returns> public static bool SetCustomPropertyByCommonFile(string filePath, Dictionary <string, object> dicKeyValue) { if (string.IsNullOrEmpty(filePath) || dicKeyValue == null || dicKeyValue.Count == 0) { return(false); } try { DSOFile.OleDocumentPropertiesClass sd = new OleDocumentPropertiesClass(); sd.Open(filePath, false, dsoFileOpenOptions.dsoOptionDefault); foreach (string key in dicKeyValue.Keys) { object value = dicKeyValue[key]; if (value == null) { value = ""; } if (IsExistsCustomProperty(sd.CustomProperties, key) == false) { sd.CustomProperties.Add(key, ref value); } else { sd.CustomProperties[key].set_Value(ref value); } } sd.Save(); sd.Close(true); } catch (Exception ex) { string msg = GetExceptionMessage(ex); string WriteFilePropertyLogDir = AppDomain.CurrentDomain.BaseDirectory + ConfigurationSettings.AppSettings["WriteFilePropertyLogDir"]; StreamWriter write = new StreamWriter(WriteFilePropertyLogDir + "/WriteFilePropertyErrorLog.txt", true, Encoding.Default); write.WriteLine(DateTime.Now.ToString() + ",调用SetCustomPropertyByCommonFile方法执行异常,写扩展属性的文件:" + filePath + ",异常信息:" + msg); write.Close(); write.Dispose(); throw ex; } return(true); }
private static void ChangeFileCustomProperties() { var doc = new OleDocumentPropertiesClass(); try { doc.Open(@"C:\test danny.txt"); //doc.SummaryProperties.Company = "ComboxTest"; doc.CustomProperties.Add("ComboxManager", Guid.NewGuid().ToString()); } catch (Exception ex) { CLLogger.LogError(ex); Console.WriteLine(ex.Message); ex = null; } //after making changes, you need to use this line to save them doc.Save(); }
/// <summary> /// 上传 /// </summary> /// <param name="context"></param> private void UpLoad(HttpContext context) { string path = context.Server.MapPath(context.Request["value1"]); HttpFileCollection files = context.Request.Files; long allSize = 0; for (int i = 0; i < files.Count; i++) { allSize += files[i].ContentLength; } if (allSize > 20 * 1024 * 1024) { context.Response.Write("文件大小超过限制"); } for (int i = 0; i < files.Count; i++) { files[i].SaveAs(path + Path.GetFileName(files[i].FileName)); //设置文件上传者到作者字段 string userid = context.Request["user"];//获取操作员 ////creates new class of oledocumentproperties var doc = new OleDocumentPropertiesClass(); //open yout selected file string pathToFile = path + Path.GetFileName(files[i].FileName); doc.Open(pathToFile, false, dsoFileOpenOptions.dsoOptionDefault); //you can set properties with summaryproperties.nameOfProperty = value; for example doc.SummaryProperties.Author = userid; // after making changes, you need to use this line to save them doc.Save(); doc.Close(); } context.Response.Write("OK"); }