简体   繁体   English

用C#进行文件读写

[英]File reading and writing in c#

My Code is 我的代码是

        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;
        }

It is giving error as: 它给出错误为:

The process cannot access the file 'D:\\Projects\\Mtelligence\\Mtelligence.Web\\App_Themes\\Clone_ForCloneTest_-1\\Clone_ForCloneTest_-1.css' because it is being used by another process. 该进程无法访问文件'D:\\ Projects \\ Mtelligence \\ Mtelligence.Web \\ App_Themes \\ Clone_ForCloneTest_-1 \\ Clone_ForCloneTest_-1.css,因为该文件正在被另一个进程使用。

Please Help me .......... how to solve this 请帮助我........如何解决这个问题

Iam trying to read .css,.skin files from a folder and write those same files in another folder with different name IAM尝试从一个文件夹中读取.css,.skin文件,并将相同的文件写入另一个具有不同名称的文件夹中

Look this: 看看这个:

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

Actually File.Create dos not just create a file but it creates the file and returns an open stream to it. 实际上, File.Create不仅可以创建文件,还可以创建文件并向其返回开放流。 Your subsequent call to File.WriteAllText will try to write a file open by yourself. 您随后对File.WriteAllText调用将尝试编写一个自己打开的文件。 In you case (because you do not use streams returned by File.Create ) simply remove them: 在这种情况下(因为您不使用File.Create返回的File.Create ),只需删除它们:

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

From the error you are getting I imagine that you are not closing a stream to the file you are manipulating. 从错误中您将得到,我想象您没有关闭要处理的文件的流。 Possibly the lines: 可能的行:

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

They return a FileStream object that you should probably flush and close when you are finished with it. 它们返回一个FileStream对象,完成后可能应该刷新并关闭该对象。

Remember it is rude not to flush :) 记住不冲洗是不礼貌的:)

So do this for your Creating your files: 为此,请执行以下操作以创建文件:

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

Is there any reason you're not using File.Copy ? 有什么原因不使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM