【发布时间】:2008-09-24 23:34:03
【问题描述】:
我有一个程序,它使用如下方法将一些数据写入文件。
public void ExportToFile(string filename)
{
using(FileStream fstream = new FileStream(filename,FileMode.Create))
using (TextWriter writer = new StreamWriter(fstream))
{
// try catch block for write permissions
writer.WriteLine(text);
}
}
运行程序时出现错误:
未处理的异常:System.UnauthorizedAccessException:对路径“mypath”的访问被拒绝。 在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, nt32 权限、布尔用户权限、文件共享共享、Int32 缓冲区大小、文件选项 选项,SECURITY_ATTRIBUTES secAttrs,字符串 msgPath,布尔 bFromProxy) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问 FileShare 共享、Int32 bufferSize、FileOptions 选项、String msgPath、Boolea bFromProxy)
问题:我需要什么代码来捕获它以及如何授予访问权限?
【问题讨论】: