2017-06-25 33 views
-1

我堅持我的程序在C#中。所以用戶必須按一個按鈕來創建一個隨機字符串(工作正常),然後他可以選擇點擊另一個按鈕。這個打開一個filedialog並讓他選擇一個他想要重命名爲隨機字符串的dll文件。我無法得到它的工作。它說我的dll已經在另一個進程中運行(不是)。任何幫助非常感謝:)c#/重命名DLL使用OpenFileDialog

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; 
     } 
+0

'至極是not'你覺得NET和操作系統在騙你? Nothinkg的好處可以讓用戶重新命名DLL。請閱讀[問]並參加[導遊] – Plutonix

+2

是的。 _You_通過創建一個StreamReader來使用它。無論如何,StreamReader有什麼意義? –

+0

重命名.dlls基本上將它們孤立起來。加** kernel32.dll **會得到一個脾氣暴躁的 – MickyD

回答

1

這是因爲您的StreamReader打開文件,因此它不能被移動。這條線似乎沒有做任何事情,所以你可以刪除它。理想與

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

謝謝你的工作很棒:) –

1

替換它只是發表意見streem讀者線的初始化或明年將它移動到重命名線,但不要忘了通過新的名稱