2013-07-29 25 views
0

我有一個windows窗體應用程序與backgroundworker(bgw)。 這BGW做一些任務,這些任務中,有這些: 第2步 - 復​​制文件從一個文件夾到另一個,使用下面的代碼:C#Winforms BackgroundWorker複製和更改文件System.IO .__ Error.WinIOError

public static void CopiarArquivos() 
    { 
     string fileName; 
     string sourcePath = System.Windows.Forms.Application.StartupPath; 
     const string targetPath = @"C:\CWEB\CwebIntegracaoMovel"; 

     if (sourcePath == targetPath) return; 

     string destFile; 

     // To copy a folder's contents to a new location: 
     // Create a new target folder, if necessary. 
     if (!Directory.Exists(targetPath)) 
     { 
      Directory.CreateDirectory(targetPath); 
     } 

     if (Directory.Exists(sourcePath)) 
     { 
      string[] files = Directory.GetFiles(sourcePath); 
      //Invoke((MethodInvoker)(() => { 

      //}); 
      // Copy the files and overwrite destination files if they already exist. 
      foreach (string s in files) 
      { 
       FileInfo fi = new FileInfo(s); 

       // Use static Path methods to extract only the file name from the path. 
       fileName = Path.GetFileName(s); 
       destFile = Path.Combine(targetPath, fileName); 
       if (!ArquivosIguais(s, destFile) && 
        (!fileName.Contains("WizardIntegracao.exe") || 
        (fileName.Contains("WizardIntegracao.exe") && !File.Exists(destFile)))) 
       { 
        fi.CopyTo(destFile, true); 
        //File.Copy(s, destFile, true); 
        //new System.Security.Permissions.FileIOPermission(
        // System.Security.Permissions.FileIOPermissionAccess.Read, new string[] { destFile }).Demand(); 
       } 
      } 
     } 
    } 

複製工作正常(有一些行註釋掉正如我測試了一些其他的事情) 在BGW的步驟4,I更改這是在步驟2中複製.config文件一些設置,用下面的代碼:

public static void AlteraBaseAddress(string porta) 
    { 
     string path = "C:\\CWEB\\CwebIntegracaoMovel\\ServicoIntegracao.exe.config"; 
     string conteudo = File.ReadAllText(path); 
     int inicio = conteudo.IndexOf("http://"); 
     int fim = conteudo.IndexOf("/ServicoIntegracao/Servico"); 
     conteudo = conteudo.Remove(inicio, fim - inicio); 
     conteudo = conteudo.Insert(inicio, String.Format("http://localhost:{0}", porta)); 

     File.WriteAllText(path, conteudo); 
    } 

當運行我的申請,我得到的例外:

Ocorreu um erro no processo de instalação do serviço: 
O acesso ao caminho 'C:\CWEB\CwebIntegracaoMovel\ServicoIntegracao.exe.config' foi negado.. 
    em System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    em System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) 
    em System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) 
    em System.IO.StreamWriter.CreateFile(String path, Boolean append) 
    em System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) 
    em System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding) 

雖然在嘗試更改.config文件的內容時發生錯誤,但據我所能研究,複製文件時發生錯誤,它們被我自己的應用程序鎖定,然後我無法覆蓋它的內容。

有誰知道如何使這項工作?

+0

你應該把例外翻譯成英文,不要理解它所說的一個字。 –

+0

@HansPassant這個.config文件不是我正在運行的主要應用程序,它是我的應用程序安裝的Windows服務(在做其他事情時) –

+0

@KingKing對不起,因爲沒有將它翻譯成英文,所以這是「The訪問路徑'...'被拒絕...「 –

回答

0

要更改應用程序配置,您應該使用System.Configuration.Configuration類。

有關如何使用Save方法執行此操作的完整示例。

http://msdn.microsoft.com/en-us/library/ms134088.aspx

祝你好運與你的追求。

+0

我實際上沒有嘗試使用Configuration類,但嘗試實例化XmlDocument來操作屬性並使用xmlDoc.Save(...)保存它; 我會嘗試你的建議...謝謝... –

+0

它確實工作,以改變connectionString,但由於這個文件是一個Windows服務配置文件,我找不到如何更改其中的其他標籤。我想更改baseAddress屬性。我會在這裏做一些搜索... –

相關問題