在我的表格中,我有这个:
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;
fileout = "D://Download/Scuola/C++/Visual Studio/genericFileName.txt"; //crea un file e ci mette la roba criptata
textFileScelto.Text = filein;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
我想知道两件我不知道如何做的事情......
对于fileout,如何调出SaveFileDialog而不是手动输入程序中的位置(编号:fileout = "D: // downloads / School / C ++ / Visual Studio / genericFileName.txt"
)?
如何确保fileout与filein的格式相同? (例如,如果我在file.exe中选择文件,则文件输出必须已.exe
,我必须只选择名称。
在此先感谢,我为任何语法错误道歉。我希望我能理解你的答案。
修改 我解决了我的第一个问题(.1),但现在出现了另一个问题:
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);
}
}
现在,当我输入要保存的文件的名称时,它会给我一个错误:进程无法访问文件(目录+ fileout的名称)因为它正被另一个进程使用。 :(为什么?
答案 0 :(得分:1)
1)SaveFileDialog(与OpenFileDialog大致相同)
2)只需设置SaveFileDialog选项以强制扩展。您也可以在用户退出对话框后自行验证和/或强制扩展。