1
votes

I am getting the below error while trying to write to a file on the system. The code was working till time and all of a sudden I am continuously getting this error. Can anyone let me know as to what might have gone wrong?

Error:

Thread was being aborted.

Stack Trace:

at Microsoft.Win32.Win32Native.CreateFile(String lpFileName, Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile)

at Microsoft.Win32.Win32Native.SafeCreateFile(String lpFileName, Int32 dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, Int32 dwFlagsAndAttributes, IntPtr hTemplateFile)

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, Boolean useLongPath)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, 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..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)

at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)

at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding)

at MyModule.TrySaveFile(String fileName, StringBuilder sb)

In my code I am using this line:

File.WriteAllText(fileName, sb.ToString());
1
Does your application have access to the file now? Do you set the appropriate permissions? It seems that something (perhaps the OS) is denying access to the file and terminating the accessing process. Do you also happen to access a file from a removable drive that is not mounted, or gets unmounted while writing to?Ivaylo Slavov
Seems that you don't have permissions to create a file, or the file exists and you don't have permissions to replaces it, or you have it oppened so you can't modify it because is protected by the threat that has the file opennedAlberto León
Hi, Thanks for the response. From the application was able to write it to the folder all this while and suddenly it stopped working. And an IIS reset was tried on the server which actually resolved the issue. So does not seem to be an access issue. But I would still like to understand the root cause for the issue.user2322482
Can someone let me know the different scenarios when "Thread abort Exception" might occur while trying to write to a file? Also what are the scenarios when an IIS reset might resolve the issue? Just to add, I am writing to a file from a web application and hence an IIS reset was done :)user2322482
Hi, Can someone please let me know the possible reasons as I am facing this issue again.user2322482

1 Answers

-1
votes

You need to get the access to create the file from application.