using (StreamWriter fsDecrypted = new StreamWriter(sOutputFilename))为什么总是提示有另一进程在操作啊!实际上该文件并没有被占用。可以打开重新写入数据
并保存的啊!
        /// <summary>   
        /// 文件解密
        /// </summary>   
        /// <param name="sInputFilename">要解密的文件</param>   
        /// <param name="sOutputFilename">解决后保存的文件</param>   
        /// <param name="sKey">密钥</param>   
        public 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.   
            using (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);             
                using (StreamWriter fsDecrypted = new StreamWriter(sOutputFilename))
                {
                    fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
                    fsDecrypted.Flush();
                    cryptostreamDecr.Dispose();
                    fsDecrypted.Dispose();
                    cryptostreamDecr.Close();
                    fsDecrypted.Close();
                }
            }        }
    

解决方案 »

  1.   

    你里面两次用到了流打开 sOutputFilename
    using (FileStream fsread =
    using (StreamWriter fsDecrypted 
    而且上一次还没有释放
      

  2.   

    StreamWriter尽量和StreamReader配对,另外,不要嵌套using,分开写试试
      

  3.   

    using System;
    using System.IO;
    using System.Text;class Test 
    {

        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";        // Delete the file if it exists.
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }        //Create the file.
            using (FileStream fs = File.Create(path)) 
            {
                AddText(fs, "This is some text");
                AddText(fs, "This is some more text,");
                AddText(fs, "\r\nand this is on a new line");
                AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");            for (int i=1;i < 120;i++) 
                {
                    AddText(fs, Convert.ToChar(i).ToString());                //Split the output at every 10th character.
                    if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) 
                    {
                        AddText(fs, "\r\n");
                    }
                }
            }        //Open the stream and read it back.
            using (FileStream fs = File.OpenRead(path)) 
            {
                byte[] b = new byte[1024];
                UTF8Encoding temp = new UTF8Encoding(true);
                while (fs.Read(b,0,b.Length) > 0) 
                {
                    Console.WriteLine(temp.GetString(b));
                }
            }
        }    private static void AddText(FileStream fs, string value) 
        {
            byte[] info = new UTF8Encoding(true).GetBytes(value);
            fs.Write(info, 0, info.Length);
        }
    }
      

  4.   

    fsread 关闭之后在这里就不能打开了。
    fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
      

  5.   

    fsread 关闭之后在这里就不能打开了。
    fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
      

  6.   

    读取时改为 FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
      

  7.   


    是的,但是如果把fsread close之后
    fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
    就无法打开了。
    如果重新建立StreamReader还是会报同样的错,请高手指点,谢谢!
      

  8.   

    那就这样定义文件流
    FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
      

  9.   

    主要的问题在这里:
    using (StreamWriter fsDecrypted = new StreamWriter(sOutputFilename))
                    {
                        fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
    前面可以读了,后面这么写啊!还是一样的错误,读的进程占用了。
      

  10.   

    FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
    下面是如何写?
    StreamWriter fsDecrypted = new StreamWriter(sOutputFilename))
    fsDecrypted.Write(sr);
    还是会报一样的错误啊!另一进程正在使用
      

  11.   

     using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))改成
     using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read,FileShare.ReadWrite))  CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);改成
      CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read,-----);//--不知道有没有打开共享锁的像FileShare.ReadWrite这种
      

  12.   

    这问题又出来,
     fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());在这里的时候文件报错提示,不正确的数据,这是为啥?
      

  13.   

    文件可以读写了,也可以解密了,只是解密后内容却成了System.IO.StreamReader,这完全不是想要的结果,这是为什么,请高手指点一下,谢谢!using (FileStream fsread = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))          
                {
                    //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.                      
                    StreamReader sr = new StreamReader(cryptostreamDecr, System.Text.Encoding.Default);
                    sr.ReadToEnd();
                    using (StreamWriter fsDecrypted = new StreamWriter(sOutputFilename))
                    {
                      fsDecrypted.Write(sr);
                        fsDecrypted.Flush();                  
                       sr.Dispose();
                        fsDecrypted.Dispose();
                        fsread.Dispose();                 
                        sr.Close();                   
                        fsread.Close();                    
                        fsDecrypted.Close();
                    }