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();
}
} }
并保存的啊!
/// <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();
}
} }
解决方案 »
- 一行30几个错
- 子窗体设置为自动充满父窗体后,子窗体中的imagebox的实际长宽就无法获取了?
- 鼠标和键盘模拟,怎么输入字符串
- 我是新手
- 如何在菜单上加上图标
- 烦请winform高手帮忙:form.FormBorderStyle = none 后,如何鼠标调整窗口大小?
- webform有能插入图片,文字,表格的控件吗?没有的话,如何做?
- access 备注类型,为什么插入一点数据,asp.net查询就报错?
- System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Select, true)]
- DevExpress中barManager下的toolbar如何在panel中显示
- 程序用存储过程向数据库添加数据,怎么样反回具体错误信息
- gridviw第一行第三列取不到值
using (FileStream fsread =
using (StreamWriter fsDecrypted
而且上一次还没有释放
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);
}
}
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
是的,但是如果把fsread close之后
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
就无法打开了。
如果重新建立StreamReader还是会报同样的错,请高手指点,谢谢!
FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
using (StreamWriter fsDecrypted = new StreamWriter(sOutputFilename))
{
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
前面可以读了,后面这么写啊!还是一样的错误,读的进程占用了。
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
下面是如何写?
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename))
fsDecrypted.Write(sr);
还是会报一样的错误啊!另一进程正在使用
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这种
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());在这里的时候文件报错提示,不正确的数据,这是为啥?
{
//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();
}