2012-07-06 96 views
1

我的代碼是文件閱讀和C#編寫

 try 
     { 
      string mtellThemePath = ConfigurationManager.AppSettings["mtellThemePath"] == null ? Server.MapPath("~/App_Themes/") : ConfigurationManager.AppSettings["mtellThemePath"].Contains("%%") ? Server.MapPath("~/App_Themes") : ConfigurationManager.AppSettings["mtellThemePath"]; 

      string[] folderPaths = Directory.GetDirectories(mtellThemePath); 
      string currentSurveyThemeName = hfSurveyName.Value + "_" + hfClientId.Value; 
      string cloneSurveyThemeName = lstSurvey[0].SurveyName + "_" + Identity.Current.UserData.ClientId; 
      string cloneSurveyThemePath = mtellThemePath + "\\" + lstSurvey[0].SurveyName + "_" + Identity.Current.UserData.ClientId; 

      string cssFile = cloneSurveyThemePath + "\\" + cloneSurveyThemeName + ".css"; 
      string skinFile = cloneSurveyThemePath + "\\" + cloneSurveyThemeName + ".skin"; 

      string FileContentCSS = string.Empty; 
      string FileContentSkin = string.Empty; 

      foreach (string oFolder in folderPaths) 
      { 
       if (oFolder.Split('\\')[5] == currentSurveyThemeName) 
       { 
        string[] cssSkinFiles = Directory.GetFiles(oFolder); 
        foreach (string objFile in cssSkinFiles) 
        { 
         if (objFile.Split('\\')[6].Split('.')[1] == "css") 
         { 
          FileContentCSS = File.ReadAllText(objFile); 
         } 
         if (objFile.Split('\\')[6].Split('.')[1] == "skin") 
         { 
          FileContentSkin = File.ReadAllText(objFile); 
         } 
        } 
       } 
      } 
      Directory.CreateDirectory(cloneSurveyThemePath); 
      File.Create(cssFile); 
      File.Create(skinFile); 
      if (FileContentCSS != string.Empty) 
      { 
       File.WriteAllText(cssFile, FileContentCSS); 
      } 
      if (FileContentSkin != string.Empty) 
      { 
       File.WriteAllText(skinFile, FileContentSkin); 
      } 
      return lstSurvey[0].SurveyGuid; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

這是給錯誤爲:

該進程無法訪問該文件 「d:\項目\ Mtelligence \ Mtelligence.Web \ App_Themes \ Clone_ForCloneTest_-1 \ Clone_ForCloneTest_-1.css' ,因爲它正在被另一個進程使用。

請幫我.......... 如何解決這個

蔭試圖讀取的CSS,從一個文件夾。皮膚文件,並在另一個文件夾寫那些相同的文件不同的名稱

+0

如果您沒有儘可能清楚地解釋您正在嘗試做什麼,您將無法得到答案。通過提交源代碼並指出錯誤是假設其他開發人員會明白你在做什麼,但實際上他們所做的只是對你投票。我還沒有投票贊成你,我試圖給你一些反饋意見,以便你能夠儘早找到你的問題的根源。 – 2012-07-06 12:35:48

+0

Iam正在嘗試從文件夾中讀取.css,.skin文件並將這些文件以不同名稱寫入另一個文件夾中 – user1309790 2012-07-06 12:38:01

+1

您是否在文本編輯器中打開文件?錯誤信息是非常明顯的,你需要給出更多的信息,看看問題是什麼(無法​​找到其他程序的內容?想要打開它,即使它正在使用中?它看着錯誤的文件?是否需要把事情縮小一點... – Chris 2012-07-06 12:40:48

回答

5

看看這個:

File.Create(cssFile); 
File.Create(skinFile); 
if (FileContentCSS != string.Empty) 
{ 
    File.WriteAllText(cssFile, FileContentCSS); 
    // ... 
} 

其實File.Create DOS不只是創建文件,但會創建該文件並向其返回一個打開的流。您隨後致電File.WriteAllText將嘗試寫入自己打開的文件。在你的情況下(因爲你不使用由File.Create返回流)簡單地將其刪除:

if (FileContentCSS != string.Empty) 
{ 
    File.WriteAllText(cssFile, FileContentCSS); 
    // ... 
} 
+0

非常感謝你.....它解決了... – user1309790 2012-07-06 12:43:26

+0

如果它解決了您的問題,請不要忘記標記爲答案。 – 2012-07-06 12:45:57

4

從你正在得到的錯誤我想你是不是正在關閉你正在操作的文件的流。可能是線路:

> File.Create(cssFile); 
> File.Create(skinFile); 

他們返回你應該沖洗和關閉時,你就完成了它FileStream對象。

記住它是粗魯不刷新:)

所以做此爲您創建您的文件:

using (var stream = File.Create(cssFile)) 
{ 
    // do your tasks here 
    stream.Flush(); 
} 
+0

ya如何這樣做..請把解決方案代碼 – user1309790 2012-07-06 12:40:04

+1

我不認爲在這種情況下File.Create是必需的,但嘗試使用using語句,而不是stream.Close();它將防止異常 – 2012-07-06 12:44:01

+0

是的使用語句更好;我認爲阿德里亞諾已經更具體地回答了這個問題。 – 2012-07-06 12:44:48

3

是否有您不使用任何File.Copy原因?

// To copy a file to another location and 
    // overwrite the destination file if it already exists. 
    System.IO.File.Copy(sourceFile, destFile, true);