在readonly xml文件中序列化数据集

时间:2013-02-26 11:03:56

标签: c# file-io xml-serialization

我有一个DataSet,我在加密模式的xml文件中序列化。 目前正在srinivasganaparthi关注以加密模式创建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似乎无效。我怎么能只读这个?

0 个答案:

没有答案