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”,因为它正被另一个进程使用。

请帮助我..........如何解决这个问题

我正在尝试从一个文件夹中读取 .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 回答