C# fileStream保存文件会丢失属性

1.我使用DSOFile对文件A设置了自定义属性fileId。

2.我使用fileStream读取A文件的所有内容,将读取的内容保存为B文件。

3.检测B文件,发现fileId属性不存在。

由此发现fileStream的读取->写入操作会丢失文档的属性,那么,请问是否有不丢失文档属性的写入方案?

下面是我用到的读写代码:

// FileStream读取文件
public static string FileStreamReadFile(string filePath)
{
    byte[] data = new byte[100];
    char[] charData = new char[100];
        FileStream file = new FileStream(filePath, FileMode.Open);
        //文件指针指向0位置
        file.Seek(0, SeekOrigin.Begin);
        //读入两百个字节
        file.Read(data, 0, (int) file.Length);
        //提取字节数组
        Decoder dec = Encoding.UTF8.GetDecoder();
        dec.GetChars(data, 0, data.Length, charData, 0);
          
    return Convert.ToString(charData);
}
// 用FileStream写文件
public static void FileStreamWriteFile(string filePath, string str)
{
    byte[] byData;
    char[] charData;
    try
    {
        FileStream nFile = new FileStream(filePath + "love.txt", FileMode.Create);
        //获得字符数组
        charData = str.ToCharArray();
        //初始化字节数组
        byData = new byte[charData.Length];
        //将字符数组转换为正确的字节格式
        Encoder enc = Encoding.UTF8.GetEncoder();
        enc.GetBytes(charData, 0, charData.Length, byData, 0, true);
        nFile.Seek(0, SeekOrigin.Begin);
        nFile.Write(byData, 0, byData.Length);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
阅读 3.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进