1

私のコードは

        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:\ Projects \ Mtelligence \ Mtelligence.Web \ App_Themes \ Clone_ForCloneTest_-1\Clone_ForCloneTest_-1.css'にアクセスできません。

私を助けてください..........これを解決する方法

Iamは、フォルダーから.css、.skinファイルを読み取り、それらの同じファイルを別の名前の別のフォルダーに書き込もうとしています。

4

3 に答える 3

5

これを見て:

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

実際File.Createには、ファイルを作成するだけでなく、ファイルを作成して、開いているストリームを返します。に続く呼び出しはFile.WriteAllText、自分で開いたファイルを書き込もうとします。あなたの場合(によって返されたストリームを使用しないためFile.Create)、単にそれらを削除します。

if (FileContentCSS != string.Empty)
{
    File.WriteAllText(cssFile, FileContentCSS);
    // ...
}
于 2012-07-06T12:39:41.183 に答える
4

あなたが得ているエラーから、あなたが操作しているファイルへのストリームを閉じていないと私は想像します。おそらく行:

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

それらはFileStreamオブジェクトを返します。これは、使い終わったらフラッシュして閉じる必要があります。

フラッシュしないのは失礼だということを忘れないでください:)

したがって、ファイルの作成に対してこれを行います。

using (var stream = File.Create(cssFile))
{
    // do your tasks here
    stream.Flush();
}
于 2012-07-06T12:39:04.950 に答える
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);
于 2012-07-06T12:42:17.430 に答える