C# 在只读xml文件中序列化数据集

C# 在只读xml文件中序列化数据集,c#,file-io,xml-serialization,C#,File Io,Xml Serialization,我有一个数据集,我正在以加密模式在xml文件中序列化它。 当前要在加密模式下创建xml。我还想使文件成为只读文件。为了实现这一点。我正在尝试: public static void EncryptAndSerialize(Object obj) { UnicodeEncoding aUE = new UnicodeEncoding(); byte[] key = aUE.GetBytes("password"); RijndaelManaged RMCrypto = ne

我有一个
数据集
,我正在以加密模式在
xml
文件中序列化它。 当前要在加密模式下创建
xml
。我还想使文件成为只读文件。为了实现这一点。我正在尝试:

public static void EncryptAndSerialize(Object obj)
{
    UnicodeEncoding aUE = new UnicodeEncoding();
    byte[] key = aUE.GetBytes("password");
    RijndaelManaged RMCrypto = new RijndaelManaged();
    using (FileStream fs = File.Open(@"D:\Sample.xml", FileMode.Create))
    {
    using (CryptoStream cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key,   key), CryptoStreamMode.Write))
        {
            XmlSerializer xmlser = new XmlSerializer(obj.GetType());
            xmlser.Serialize(cs, obj);
        }
        fs.Close();
        File.SetAttributes(@"D:\Sample.xml",FileAttributes.ReadOnly);
}

但是
File.SetAttributes
似乎不起作用。如何将其设置为只读?

在使用部分将其从
移动后,尝试使用相同的设置属性