Hi All,
I have a question regarding FileStream and StreamWriter. I am doing
the below IO operation in my project .But, it throws the error of "The
process cannot access the file because it is used by another process"
in below highlighted line.
string filePath = "C:/Test/Folder1"
string fileName =
DateTime.Today.Day.ToString() + DateTime.Today.Month.ToString() +
DateTime.Today.Year.ToString();
string fullFilePath = filePath + "_" +
fileName + ".txt";
FileStream fs = new
FileStream(fullFilePath, FileMode.Append, FileAccess.Write);
if (!File.Exists(fullFilePath))
{
fs = new FileStream(fullFilePath,
FileMode.CreateNew, FileAccess.Write);
}
StreamWriter sw = new StreamWriter(fs);
sw.Write("Test" );
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
So, If you know the solution then kindly reply me only. Thanks in advance..:)
Thanks & Regards,
Sanjiv
SOLUTION 1:
Hi Sanjiv,
You have to use using keyword to avoid such access behaviors
string filePath = "C:/Test/Folder1";
string fileName = DateTime.Today.Day.ToString() +
DateTime.Today.Month.ToString() + DateTime.Today.Year.ToString();
string fullFilePath = filePath + "_" + fileName + ".txt";
FileMode _fm = FileMode.Append;
if (!File.Exists(fullFilePath))
_fm = FileMode.CreateNew;
using (FileStream fs = new FileStream(fullFilePath, _fm,
FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write("Test");
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
}
More reference
• http://msdn.microsoft.com/en-us/library/yh598w02.aspx
• http://www.codeproject.com/Articles/17106/The-using-Keyword-in-C
-Oli.
I have a question regarding FileStream and StreamWriter. I am doing
the below IO operation in my project .But, it throws the error of "The
process cannot access the file because it is used by another process"
in below highlighted line.
string filePath = "C:/Test/Folder1"
string fileName =
DateTime.Today.Day.ToString() + DateTime.Today.Month.ToString() +
DateTime.Today.Year.ToString();
string fullFilePath = filePath + "_" +
fileName + ".txt";
FileStream fs = new
FileStream(fullFilePath, FileMode.Append, FileAccess.Write);
if (!File.Exists(fullFilePath))
{
fs = new FileStream(fullFilePath,
FileMode.CreateNew, FileAccess.Write);
}
StreamWriter sw = new StreamWriter(fs);
sw.Write("Test" );
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
So, If you know the solution then kindly reply me only. Thanks in advance..:)
Thanks & Regards,
Sanjiv
SOLUTION 1:
Hi Sanjiv,
You have to use using keyword to avoid such access behaviors
string filePath = "C:/Test/Folder1";
string fileName = DateTime.Today.Day.ToString() +
DateTime.Today.Month.ToString() + DateTime.Today.Year.ToString();
string fullFilePath = filePath + "_" + fileName + ".txt";
FileMode _fm = FileMode.Append;
if (!File.Exists(fullFilePath))
_fm = FileMode.CreateNew;
using (FileStream fs = new FileStream(fullFilePath, _fm,
FileAccess.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.Write("Test");
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
}
More reference
• http://msdn.microsoft.com/en-us/library/yh598w02.aspx
• http://www.codeproject.com/Articles/17106/The-using-Keyword-in-C
-Oli.
No comments:
Post a Comment