C#生成word文件的时候,怎么自动附加只读功能

如题所述

IO读取设置啊:

            //设置文件为只读
            if (File.Exists(@"C:\Users\hd2y\Desktop\双工接口操作.docx"))
            {
                File.SetAttributes(@"C:\Users\hd2y\Desktop\双工接口操作.docx", FileAttributes.ReadOnly);
                Console.WriteLine("设置成功");
            }
            else
            {
                Console.WriteLine("文件不存在");
            }
            Console.ReadKey();

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-21

参考下面引用Spire.Doc.dll生成的Word文档,这里除了对Word文档设置只读功能限制外,也提供其他三种Word文档编辑权限的设置方法,供参考

using Spire.Doc;

namespace EditPermissions_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建一个Document类对象并加载需要设置编辑权限的Word文档
            Document doc = new Document(@"C:\Users\Administrator\Desktop\sample.docx");
                  
            //不允许任何更改(只读),设置解除限制编辑的密码
            doc.Protect(ProtectionType.AllowOnlyReading, "123");

            //只允许填写窗体,设置解除限制编辑的密码
            //doc.Protect(ProtectionType.AllowOnlyFormFields, "123");

            //只允许批注,设置解除限制编辑的密码
            // doc.Protect(ProtectionType.AllowOnlyComments, "123");

            //只允许修订,设置解除限制编辑的密码
            //doc.Protect(ProtectionType.AllowOnlyRevisions, "123");

            //保存并预览文件
            doc.SaveToFile("result.docx",FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

生成的Word文档:

相似回答