【问题标题】:c# / Rename dll using OpenFileDialogc# / 使用 OpenFileDialog 重命名 dll
【发布时间】:2017-11-28 19:20:03
【问题描述】:

我被我的 C# 程序卡住了。所以用户必须按下一个按钮来创建一个随机字符串(工作正常),然后他可以选择点击另一个按钮。这个打开一个文件对话框,让他选择一个他想重命名为随机字符串的 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;
        }

【问题讨论】:

  • wich is not 你认为 NET 和操作系统在骗你吗?无论如何,让用户重命名 DLL 是没有什么好处的。请阅读How to Ask 并采取tour
  • 是的。 通过创建StreamReader 来使用它。 StreamReader 到底有什么意义?
  • 重命名 .dll 基本上会孤立它们。再加上 kernel32.dll 会有点脾气暴躁

标签: c# dll rename openfiledialog


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    只需注释 streem reader 行的初始化或将其移动到 rename 行旁边,但不要忘记传递新名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多