小编18h*_*hrs的帖子

如何检查写入目录或文件的权限?

我有一个程序,使用如下所示的方法将一些数据写入文件.


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);


     }
}
Run Code Online (Sandbox Code Playgroud)

运行程序时出现错误:

未处理的异常:System.UnauthorizedAccessException:拒绝访问路径'mypath'.在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,nt32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions ptions,SECURITY_ATTRIBUTES secAttrs) System.IO.FileStream..ctor中的String String msgPath,Boolean bFromProxy(字符串路径,FileMode模式,FileAccess访问FileShare共享,Int32 bufferSize,FileOptions选项,字符串msgPath,Boolea bFromProxy)

问题:我需要使用哪些代码来获取此信息以及如何授予访问权限?

.net c#

60
推荐指数
5
解决办法
10万
查看次数

标签 统计

.net ×1

c# ×1