I
Ihor Bobak
Hello, everybody.
We get strange error with File.WriteAllText(), and I cannot understand why.
The stack trace is the following:
Message: The process cannot access the file
'C:\WINDOWS\TEMP\_633558601793472408_2003786287.tmp.xml' because it is being
used by another process.
Type: System.IO.IOException
Stack Trace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess
access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean
bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding
encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding
encoding)
at System.IO.File.WriteAllText(String path, String contents, Encoding
encoding)
at FrameworkBase.Log.DataPortal.LogDataPortalTargetEmail.Write(ArrayList
aLogRecords)
I am on 100% sure that the file has UNIQUE name and cannot definitely be
used by any other piece of code or any other application.
Look how it works. We have a "FileTemp" class which is responsible for
generation of the temporary file names:
public class FileTemp
{
private const string _underscore = "_";
public static string Get()
{
return Get(string.Empty, ".tmp");
}
public static string Get(string aPrefix, string aExtension)
{
string path = Path.GetTempPath();
Random random = new Random();
string filename;
int count = 0;
do
{
++count;
filename = path + StringUtils.StringSuffixForce(aPrefix, "_") +
DateTime.Now.Ticks.ToString() + "_" + random.Next() +
StringUtils.StringPrefixForce(aExtension, ".");
}
while (count < 100 && File.Exists(filename));
if (count == 100)
throw new ExceptionFramework("Cannot generate filename after 100
attempts. Something impossible...", ExceptionKind.ekDeveloper);
return filename;
}
}
when it generates a name, it checks it's uniqueness. If after 100 attempts
it cannot generate it, it fails with our exception. BUT WE NEVER GOT IT
FAILED SINCE WE EXIST. And neither it failed this time.
Next, the code with File.WriteAllText is the following:
string xmlFileName = FileTemp.Get() + ".xml";
// ... here we build stringWriter - nothing to do with the file
File.WriteAllText(xmlFileName, stringWriter.ToString(), Encoding.UTF8);
As I have proven, xmlFileName has unique file name (otherwise FileTemp.Get()
would fail, but it did not).
Question: why File.WriteAllText produces System.IO.IOException "The process
cannot access the file
'C:\WINDOWS\TEMP\_633558601793472408_2003786287.tmp.xml' because it is being
used by another process." ?
..NET 2.0, Windows XP SP2, windows forms application
Please, help wiith any ideas.
We get strange error with File.WriteAllText(), and I cannot understand why.
The stack trace is the following:
Message: The process cannot access the file
'C:\WINDOWS\TEMP\_633558601793472408_2003786287.tmp.xml' because it is being
used by another process.
Type: System.IO.IOException
Stack Trace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess
access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize,
FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean
bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding
encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding
encoding)
at System.IO.File.WriteAllText(String path, String contents, Encoding
encoding)
at FrameworkBase.Log.DataPortal.LogDataPortalTargetEmail.Write(ArrayList
aLogRecords)
I am on 100% sure that the file has UNIQUE name and cannot definitely be
used by any other piece of code or any other application.
Look how it works. We have a "FileTemp" class which is responsible for
generation of the temporary file names:
public class FileTemp
{
private const string _underscore = "_";
public static string Get()
{
return Get(string.Empty, ".tmp");
}
public static string Get(string aPrefix, string aExtension)
{
string path = Path.GetTempPath();
Random random = new Random();
string filename;
int count = 0;
do
{
++count;
filename = path + StringUtils.StringSuffixForce(aPrefix, "_") +
DateTime.Now.Ticks.ToString() + "_" + random.Next() +
StringUtils.StringPrefixForce(aExtension, ".");
}
while (count < 100 && File.Exists(filename));
if (count == 100)
throw new ExceptionFramework("Cannot generate filename after 100
attempts. Something impossible...", ExceptionKind.ekDeveloper);
return filename;
}
}
when it generates a name, it checks it's uniqueness. If after 100 attempts
it cannot generate it, it fails with our exception. BUT WE NEVER GOT IT
FAILED SINCE WE EXIST. And neither it failed this time.
Next, the code with File.WriteAllText is the following:
string xmlFileName = FileTemp.Get() + ".xml";
// ... here we build stringWriter - nothing to do with the file
File.WriteAllText(xmlFileName, stringWriter.ToString(), Encoding.UTF8);
As I have proven, xmlFileName has unique file name (otherwise FileTemp.Get()
would fail, but it did not).
Question: why File.WriteAllText produces System.IO.IOException "The process
cannot access the file
'C:\WINDOWS\TEMP\_633558601793472408_2003786287.tmp.xml' because it is being
used by another process." ?
..NET 2.0, Windows XP SP2, windows forms application
Please, help wiith any ideas.