System.IO.File.WriteAllText(字符串名称,字符串文本)期间线程中止错误

时间:2014-04-15 06:31:25

标签: c# io

尝试写入系统上的文件时出现以下错误。代码工作到时间,突然间我不断得到这个错误。任何人都可以告诉我可能出现的问题吗?

错误:

线程正在中止。

堆栈跟踪:

  

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)

在我的代码中,我使用的是这一行:

File.WriteAllText(fileName, sb.ToString());

1 个答案:

答案 0 :(得分:-1)

您需要获得从应用程序创建文件的权限。