CSharp自定义文件的属性

How to set custom attributes / file properties on a file in C# .Net

In the office here we have developed a document management system, which uses Custom Properties on PDF and DWG files to keep a track of what revision etc they are. In my Revit API development, we required an easy way of settings these from inside of Revit, so I set about creating a plotting external command using the new Print methods in the 2009 API and PDF995's ini file. 

Once I'd printed the documents, I wanted to rename them and set the custom properties such as Revision, drawer, designer etc. After much searching, I discovered what you need to get is "DSOFile" from Microsoft, it is intended for editing Microsoft Word document properties, but works on anything. There is an easy to understand VB.Net example included in the download. 

DSO File works by adding a reference to the DLL and creating a new DSOFile.OleDocumentPropertiesClass, and then performing actions on that. So, I created a new: private DSOFile.OleDocumentPropertiesClass m_file; Then in my constructor for my ParamaterSetter class I created an instance of OleDocumentPropertiesClass and then called the Open() Method with my filename: 

 //create new isntance of OleDocumentPropertiesClass 
m_file = new DSOFile.OleDocumentPropertiesClass(); 
// select the file you would like to open 
m_file.Open(path, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault); 

From there you can perform actions such as : m_file.CustomProperties.Add(key, ref valueForKey); where key is the name of your property, and valueForKey is the value. For more standard properties, such as the "Summary" properties (in summary tab when you go properties on the file), you can access them directly, such as: m_file.SummaryProperties.Comments = value; 

For reference, here is the utility class I use: 

class FileParameterSetter {

        //DSOFile document
        private DSOFile.OleDocumentPropertiesClass m_file;

        
        public FileParameterSetter(string path) {
            //create new isntance of OleDocumentPropertiesClass
            m_file = new DSOFile.OleDocumentPropertiesClass();

            // select the file you would like to open
            m_file.Open(path, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);

        }

        /// <summary>
        /// Sets a custom property in the file.
        /// </summary>
        /// <param name="key">key to set</param>
        /// <param name="value">value to set to key</param>
        public void SetCustomproperty(string key, string value) {
            try {
                object valueForKey; 

                //check for valid inputs
                if ((key == null) || (value == null) || (key.Length == 0) || (value.Length == 0)) {
                    //Error, no key or value.
                    return;
                }

                valueForKey = value;

                //write to file
                m_file.CustomProperties.Add(key, ref valueForKey);

                //save change
                m_file.Save();
            } catch(Exception ex) {

                System.Windows.Forms.MessageBox.Show("There was an error adding properties to PDF file: " +
                                                        ex);

            }
        }

        /// <summary>
        /// Saves and closes document
        /// </summary>
        public void Close() {
           m_file.Close(true);
        }

        /// <summary>
        /// Sets a standard property, such as Comments, Subject, Title etc
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetStandardProperty(string key, string value) {

            if (key.Equals("Comments")) {
                m_file.SummaryProperties.Comments = value;
            }
            if (key.Equals("Subject")) {
                m_file.SummaryProperties.Subject = value;
            }
            if (key.Equals("Title")) {
                m_file.SummaryProperties.Title = value;
            }
        }
        
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 WPF 中,我们也可以为自定义控件添加自定义属性,然后在 XAML 中使用该控件并设置属性值。以下是具体步骤: 1. 在自定义控件类中定义依赖属性,例如: ```csharp public static readonly DependencyProperty CustomProperty = DependencyProperty.Register("Custom", typeof(string), typeof(CustomControl), new PropertyMetadata("Default")); public string Custom { get { return (string)GetValue(CustomProperty); } set { SetValue(CustomProperty, value); } } ``` 这里定义了一个名为 Custom 的依赖属性,其类型为字符串,所有者类型为 CustomControl。默认值为 "Default"。 2. 在自定义控件的 XAML 文件中,使用该属性,例如: ```xml <UserControl x:Class="WpfApp1.CustomControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <Grid> <TextBlock Text="{Binding Custom, RelativeSource={RelativeSource AncestorType=UserControl}}" /> </Grid> </UserControl> ``` 这里使用了 Binding,将 Custom 属性绑定到 TextBlock 的 Text 属性上。 3. 在使用自定义控件的 XAML 文件中,设置该属性的值,例如: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1" Title="MainWindow" Height="450" Width="800"> <Grid> <local:CustomControl Custom="SomeValue" /> </Grid> </Window> ``` 这里在 MainWindow 中使用了 CustomControl,并设置了 Custom 属性的值为 "SomeValue"。 希望这个回答能够解决你的问题。如果还有疑问,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值