-1

Possible Duplicate:
Cant Access File because it is being used by another process

using (StreamWriter _SelectedFile = File.CreateText(CConstant.m_TEMPFILEPATH))
{
    _SelectedFile.WriteLine(CConstant.m_SaveFileDefaultDirectory);
    _SelectedFile.WriteLine(CConstant.Tempfile_ECUSelected);
    _SelectedFile.WriteLine(CConstant.Tempfile_inifile);
    _SelectedFile.WriteLine(CConstant.Tempfile_mapfile);
    _SelectedFile.Flush();
    _SelectedFile.Close();
    _SelectedFile.Dispose();
}

When running the code, the very first time when i run the code(There is no temp.txt file ) , it throws an exception "The process can't access the file, because it is being used by another process."Please suggest a solution and also what's wrong in writing the code this way?

Community
  • 1
  • 1
  • 7
    You don't need to .Flush, .Close and .Dispose. All this will be done automatically when you leave the using scope. Now you say that there is no temp.txt file that exists the first time and yet you get an exception saying that the file is used by another process. So what is it? – Darin Dimitrov Sep 02 '11 at 08:46
  • There's nothing wrong with the code (except the Flush/Close/Dispose sequence is useless). The error message is perfectly clear: another process is already using the file, so you can't write to it – Thomas Levesque Sep 02 '11 at 08:47
  • @Darin Dimitrov : yes .. the very first time , when i run the code.. there is no such file existing .. and still it pops up an exception. – Kritika Sep 02 '11 at 08:56
  • 2
    @Kritika, and you realize that this doesn't make sense? – Darin Dimitrov Sep 02 '11 at 08:57
  • This questions is already asked in SO http://stackoverflow.com/questions/321077/cant-access-file-because-it-is-being-used-by-another-process – Abdul Rahman Sep 02 '11 at 09:01

2 Answers2

2

Cant you create FileStream with FileMode.OpenOrCreate option? rather than File?

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
0

using (StreamWriter sw = new StreamWriter(CConstant.m_TEMPFILEPATH, true))

replace your code with the code of upper ,and test it, is also same result?

thetazhou
  • 62
  • 1
  • 6