适当的C#流对象

适当的C#流对象,c#,stream,C#,Stream,我可以使用StreamWriter写入哪个流对象,然后将内容作为字符串获取 StreamWriter sw= new StreamWriter(streamObject); string s = streamObject.getString (); // or something like that 编辑: 下面是完整的代码,我不想写入文件,而是想写入内存中的流对象,然后以字符串形式获取内容: static void DecryptFile(string sInputFilename,

我可以使用StreamWriter写入哪个流对象,然后将内容作为字符串获取

StreamWriter sw= new StreamWriter(streamObject);
string s = streamObject.getString (); // or something like that
编辑: 下面是完整的代码,我不想写入文件,而是想写入内存中的流对象,然后以字符串形式获取内容:

  static void DecryptFile(string sInputFilename,
      string sOutputFilename,
      string sKey) {
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            //A 64 bit key and IV is required for this provider.
            //Set secret key For DES algorithm.
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            //Set initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            //Create a file stream to read the encrypted file back.
            FileStream fsread = new FileStream(sInputFilename,
               FileMode.Open,
               FileAccess.Read);
            //Create a DES decryptor from the DES instance.
            ICryptoTransform desdecrypt = DES.CreateDecryptor();
            //Create crypto stream set to read and do a 
            //DES decryption transform on incoming bytes.
            CryptoStream cryptostreamDecr = new CryptoStream(fsread,
               desdecrypt,
               CryptoStreamMode.Read);
            //Print the contents of the decrypted file.
            StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
            fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
            fsDecrypted.Flush();
            fsDecrypted.Close();
        } 

根据您的编辑,而不是以下内容:

//Print the contents of the decrypted file.
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
你可以这样写:

static string DecryptFile(string sInputFilename, string sKey)
{
    using (var DES = new DESCryptoServiceProvider())
    {
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

        using (var fsread = new FileStream(sInputFilename, FileMode.Open,FileAccess.Read))
        using (var desdecrypt = DES.CreateDecryptor())
        using (var cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read))
        using (var reader = new StreamReader(cryptostreamDecr))
        {
            // this is a stream content as a string, you don't need to write and read it again
            return reader.ReadToEnd();
        }
    }
}

还要注意的是,您的代码在使用s进行IDisposable实现时遗漏了。

您想在这里做什么?StreamWriter的存在是为了在流上写入,似乎您正在尝试从底层流读取如果您需要获取字符串,您应该使用StreamReader()@Steve YES,我想要一个可以使用StreamWriter写入的流对象,然后获取内容,然后完成写入,然后使用StreamReader重新打开。但是,如果您刚开始在该文件中写入,您已经知道您正在写入的内容。请使用打开流并自动关闭它。
static string DecryptFile(string sInputFilename, string sKey)
{
    using (var DES = new DESCryptoServiceProvider())
    {
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

        using (var fsread = new FileStream(sInputFilename, FileMode.Open,FileAccess.Read))
        using (var desdecrypt = DES.CreateDecryptor())
        using (var cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read))
        using (var reader = new StreamReader(cryptostreamDecr))
        {
            // this is a stream content as a string, you don't need to write and read it again
            return reader.ReadToEnd();
        }
    }
}