文件在c#中读写

时间:2012-07-06 12:32:08

标签: c# file

我的代码是

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

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

Iam尝试从文件夹中读取.css,.skin文件,并将这些文件写入另一个名称不同的文件夹中

3 个答案:

答案 0 :(得分: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);
    // ...
}

答案 1 :(得分:4)

从你得到的错误我想象你没有关闭你正在操作的文件的流。线路可能是:

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

它们返回一个FileStream对象,你可能应该刷新并在完成它时关闭它。

记住,不要冲洗是不礼貌的。)

这样做是为了创建文件:

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

答案 2 :(得分: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);