Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用c语言读写文件_C#_File - Fatal编程技术网

C# 用c语言读写文件

C# 用c语言读写文件,c#,file,C#,File,我的代码是 try { string mtellThemePath = ConfigurationManager.AppSettings["mtellThemePath"] == null ? Server.MapPath("~/App_Themes/") : ConfigurationManager.AppSettings["mtellThemePath"].Contains("%%") ? Server.MapPath("~/App_T

我的代码是

        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\u Themes\Clone\u ForCloneTest\u1\Clone\u ForCloneTest\u1.css' 因为它正被另一个进程使用

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


我正试图从一个文件夹中读取.css、.skin文件,并将这些相同的文件以不同的名称写入另一个文件夹中

,因为您遇到了错误,我想您没有关闭您正在处理的文件的流。可能的话:

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

记住,不冲水是不礼貌的:

因此,请在创建文件时执行以下操作:

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

从您得到的错误中,我认为您没有关闭您正在处理的文件的流。可能的话:

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

记住,不冲水是不礼貌的:

因此,请在创建文件时执行以下操作:

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

File.Create(cssFile);
File.Create(skinFile);
if (FileContentCSS != string.Empty)
{
    File.WriteAllText(cssFile, FileContentCSS);
    // ...
}
实际上是文件。创建dos不仅仅是创建一个文件,它还创建一个文件并返回一个打开的流。您随后对File.WriteAllText的调用将尝试编写一个自己打开的文件。在您的情况下,因为您不使用文件返回的流。创建只需删除它们:

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

File.Create(cssFile);
File.Create(skinFile);
if (FileContentCSS != string.Empty)
{
    File.WriteAllText(cssFile, FileContentCSS);
    // ...
}
实际上是文件。创建dos不仅仅是创建一个文件,它还创建一个文件并返回一个打开的流。您随后对File.WriteAllText的调用将尝试编写一个自己打开的文件。在您的情况下,因为您不使用文件返回的流。创建只需删除它们:

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

你不使用File.Copy有什么原因吗


你不使用File.Copy有什么原因吗


如果你不尽可能清楚地解释你想做什么,你就不会得到答案。通过提交源代码并声明错误,就是假设其他开发人员会理解您正在做的事情,而实际上他们所做的只是否决您的意见。我没有否决你,我正在尝试给你一些反馈,以便你能更快地找到问题的根源。我正在尝试从一个文件夹中读取.css、.skin文件,并将这些相同的文件以不同的名称写入另一个文件夹。你是否在文本编辑器中打开了该文件?这个错误信息是很自然的,你需要提供更多的信息来说明问题是什么,你找不到其他的程序了吗?即使在使用中也要打开它吗?它在看错误的文件?你需要把事情缩小一点…@user1309790:你有没有考虑过仅仅复制它们?如果你不尽可能清楚地解释你想做什么,你就不会得到答案。通过提交源代码并声明错误,就是假设其他开发人员会理解您正在做的事情,而实际上他们所做的只是否决您的意见。我没有否决你,我正在尝试给你一些反馈,以便你能更快地找到问题的根源。我正在尝试从一个文件夹中读取.css、.skin文件,并将这些相同的文件以不同的名称写入另一个文件夹。你是否在文本编辑器中打开了该文件?这个错误信息是很自然的,你需要提供更多的信息来说明问题是什么,你找不到其他的程序了吗?即使在使用中也要打开它吗?它在看错误的文件?你需要把事情缩小一点…@user1309790:你有没有考虑过仅仅复制它们?我不认为在这种情况下需要File.Create,但尝试使用using语句而不是stream.Close;它可以防止异常。是的,using语句更好;我认为Adriano已经更具体地回答了这个问题。我不认为在这种情况下需要File.Create,但尝试使用using语句而不是stream.Close;它可以防止异常。是的,using语句更好;我认为阿德里亚诺已经更具体地回答了这个问题。如果这个问题解决了你的问题,别忘了把这个标记为答案。如果它解决了你的问题,别忘了把这个标记为答案。