当我输入要保存的文件的名称时,它会给我一个错误:进程无法访问文件(目录+ fileout的名称)因为它正被另一个进程使用。为什么?我怎么解决?
private void button_Click_C_Open(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
filein = openFileDialog1.FileName; //file in lo scegliamo dal openfiledialog
textFileScelto.Text = filein; //visualizza la scelta in una textbox
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void Encypt_File_Click(object sender, EventArgs e)
{
try
{
Stream my1Stream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((my1Stream = saveFileDialog1.OpenFile()) != null)
{
fileout = saveFileDialog1.FileName;
passwordBytes = GetPasswordBytes();
AES.EncryptFile(filein, fileout, passwordBytes);
MessageBox.Show("File Criptato!");
my1Stream.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我为任何语法错误道歉。我希望我能理解你的答案。提前谢谢。
答案 0 :(得分:1)
由于OpenFile()方法,您自己的应用程序会锁定文件。
尝试将my1Stream.Close();
放在AES.EncryptFile(filein, fileout, passwordBytes);
之前。
实际上不需要使用流。
这应该可以解决问题
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
fileout = saveFileDialog1.FileName;
passwordBytes = GetPasswordBytes();
AES.EncryptFile(filein, fileout, passwordBytes);
MessageBox.Show("File Criptato!");
}
答案 1 :(得分:0)
没有简单的解决方案。您需要弄清楚哪个进程打开了过滤器,并停止该过程或获取退出它的顺序。
但最重要的是,您需要在代码中处理异常。
确定哪个进程以编程方式使用过滤器是一项非常重要的任务。