예제 #1
0
    /// <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);
    }
예제 #2
0
        /// <summary>
        /// 删除文件/目录操作
        /// </summary>
        /// <param name="context"></param>
        private void Delete(HttpContext context)
        {
            string[] files = context.Request["value1"].Split('|');// 分割
            foreach (string item in files)
            {
                string path = context.Server.MapPath(item);
                ////creates new class of oledocumentproperties
                var doc = new OleDocumentPropertiesClass();

                //open yout selected file
                string pathToFile = path;
                string author     = "";
                if (System.IO.File.Exists(pathToFile))
                {
                    doc.Open(pathToFile, false, dsoFileOpenOptions.dsoOptionDefault);
                    author = doc.SummaryProperties.Author;
                    doc.Close();
                    string userid = context.Request["user"];//获取操作员
                    if (author == userid)
                    {
                        if (File.Exists(path))
                        {
                            File.Delete(path);
                        }
                        else if (Directory.Exists(path))
                        {
                            Directory.Delete(path, false);
                        }
                        context.Response.Write("OK");
                    }
                }
                else
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    else if (Directory.Exists(path))
                    {
                        Directory.Delete(path, false);
                    }
                    context.Response.Write("OK");
                }
            }
        }
예제 #3
0
        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();
        }
예제 #4
0
        static void Main(string[] args)
        {
            var filePath = args.Length > 0 ? args[0] : @"C:\Users\David\Configuration.db";

            OleDocumentPropertiesClass file = new OleDocumentPropertiesClass();

            Console.WriteLine("Opening file: {0}", filePath);
            file.Open(filePath, false, dsoFileOpenOptions.dsoOptionDefault);

            SetCustomProperty(file.CustomProperties, "Packets", 65);
            SetCustomProperty(file.CustomProperties, "Powered by", "Rey David Dominguez");

            foreach (CustomProperty p in file.CustomProperties)
            {
                Console.WriteLine("{0}:{1}", p.Name, p.get_Value().ToString());
            }

            file.Close(true);
            Console.ReadKey();
        }
예제 #5
0
        /// <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");
        }
예제 #6
0
    public static object GetPropertyByDocumentType(DocumentType docType, string filePath, string propertyName)
    {
        if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(propertyName))
        {
            return(null);
        }

        propertyName = propertyName.ToLower();

        object value = null;

        if (docType == DocumentType.Word2007)
        {
            using (WordprocessingDocument document = WordprocessingDocument.Open(filePath, true))
            {
                if (document.CustomFilePropertiesPart != null)
                {
                    foreach (var oProperty in document.CustomFilePropertiesPart.Properties.Elements <CustomDocumentProperty>())
                    {
                        if (!string.IsNullOrEmpty(oProperty.Name) && oProperty.Name.ToString().ToLower() == propertyName)
                        {
                            value = oProperty.VTLPWSTR.Text;
                            break;
                        }
                    }
                }
            }
        }
        else if (docType == DocumentType.Excel2007)
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(filePath, true))
            {
                if (document.CustomFilePropertiesPart != null)
                {
                    foreach (var oProperty in document.CustomFilePropertiesPart.Properties.Elements <CustomDocumentProperty>())
                    {
                        if (!string.IsNullOrEmpty(oProperty.Name) && oProperty.Name.ToString().ToLower() == propertyName)
                        {
                            value = oProperty.VTLPWSTR.Text;
                            break;
                        }
                    }
                }
            }
        }
        else if (docType == DocumentType.PowerPoint2007)
        {
            using (PresentationDocument document = PresentationDocument.Open(filePath, true))
            {
                if (document.CustomFilePropertiesPart != null)
                {
                    foreach (var oProperty in document.CustomFilePropertiesPart.Properties.Elements <CustomDocumentProperty>())
                    {
                        if (!string.IsNullOrEmpty(oProperty.Name) && oProperty.Name.ToString().ToLower() == propertyName)
                        {
                            value = oProperty.VTLPWSTR.Text;
                            break;
                        }
                    }
                }
            }
        }
        else if (docType == DocumentType.CommonFile)
        {
            DSOFile.OleDocumentPropertiesClass sd = new OleDocumentPropertiesClass();
            sd.Open(filePath, false, dsoFileOpenOptions.dsoOptionDefault);

            if (sd != null)
            {
                foreach (CustomProperty cus in sd.CustomProperties)
                {
                    if (!string.IsNullOrEmpty(cus.Name) && cus.Name.ToLower() == propertyName)
                    {
                        value = cus.get_Value();
                        break;
                    }
                }
            }
            sd.Close(false);
        }

        return(value);
    }