使用 OpenFileDialog 重命名 dll

问题描述 投票:0回答:2

我的程序被 C# 困住了。因此,用户必须按下一个按钮来创建一个随机字符串(工作正常),然后他可以选择单击另一个按钮。这个打开一个

FileDialog
,让他选择一个他想要重命名为随机字符串的dll文件。我无法让它工作。它说我的 dll 已经在另一个进程中运行(实际上不是)。非常感谢任何帮助:)

private void btnEncrypt_Click_1(object sender, EventArgs e)
{
    // sets a random string to txtEncrypt.text
}

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog MyOpenFileDialog = new OpenFileDialog();

    //filedialog
    MyOpenFileDialog.Filter = "dll files (*.dll) |*.dll";//filter
    MyOpenFileDialog.Title = "Chose the dll file";
    MyOpenFileDialog.InitialDirectory = "C:\\Users\\Gebruiker\\Desktop";
    MyOpenFileDialog.FilterIndex = 1;
    MyOpenFileDialog.RestoreDirectory = true;

    //if ok
    if (MyOpenFileDialog.ShowDialog() == DialogResult.OK)
    {
        strPath = MyOpenFileDialog.FileName;
        StreamReader reader = new StreamReader(strPath);

        System.IO.File.Move(strPath,
            "C:\\Users\\Gebruiker\\Desktop\\" + txtEncrypt.Text + ".dll");
    }
    else //cancel
    {
        strPath = null;
    }
}
c# dll rename openfiledialog
2个回答
1
投票

这是因为您的 StreamReader 正在打开文件,因此无法移动它。无论如何,该行似乎没有执行任何操作,因此您可以将其删除。理想情况下将其替换为

if (System.IO.File.Exists(strPath))
{
    System.IO.File.Move(strPath, "C:\\Users\\Gebruiker\\Desktop\\" + txtEncrypt.Text + ".dll");
}

0
投票

只需注释流读取器行的初始化或将其移动到重命名行旁边,但不要忘记传递新名称

© www.soinside.com 2019 - 2024. All rights reserved.