Reputation: 33
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.
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
Upvotes: 1
Views: 673
Reputation: 23123
Is there any reason you're not using 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);
Upvotes: 3
Reputation: 67148
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. Your subsequent call to File.WriteAllText
will try to write a file open by yourself. In you case (because you do not use streams returned by File.Create
) simply remove them:
if (FileContentCSS != string.Empty)
{
File.WriteAllText(cssFile, FileContentCSS);
// ...
}
Upvotes: 5
Reputation: 4285
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.
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();
}
Upvotes: 4