[OpenFileDialog-saveFileDialog 错误]进程无法访问文件,因为它正被另一个进程使用. [英] [OpenFileDialog-saveFileDialog error]The process cannot access the files because it is being used by another process.

查看:28
本文介绍了[OpenFileDialog-saveFileDialog 错误]进程无法访问文件,因为它正被另一个进程使用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入要保存的文件名时,它给我一个错误:进程无法访问文件(目录+fileout的名称)因为它正在被另一个进程使用.为什么?怎么解决?

When I enter the name of the file to be saved, it gives me an error: the process cannot access the files (directory+name of fileout) Because it is being used by another process. Why? How can I solve?

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);
    }
}

对于任何语法错误,我深表歉意.我希望我能理解您的回答.提前致谢.

I apologize for any grammatical mistakes.I hope I understand well your answers. Thanks in advance.

推荐答案

由于 OpenFile() 方法,您自己的应用程序持有对文件的锁定.尝试将 my1Stream.Close(); 放在 AES.EncryptFile(filein, fileout, passwordBytes); 之前.

Your own application holds a lock to the file because of the OpenFile() method. Try to put my1Stream.Close(); before AES.EncryptFile(filein, fileout, passwordBytes);.

实际上不需要使用流.

这应该可以解决问题

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {            
        fileout = saveFileDialog1.FileName;
        passwordBytes = GetPasswordBytes();
        AES.EncryptFile(filein, fileout, passwordBytes);
        MessageBox.Show("File Criptato!");
    }

这篇关于[OpenFileDialog-saveFileDialog 错误]进程无法访问文件,因为它正被另一个进程使用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆