如何判断当前用户是否具有相应的权限
像c中的access(filename, R_OK)那样的函数有没?

解决方案 »

  1.   

    你可以使用try catch来作,捕获UnauthorizedAccessException异常
      

  2.   

    try
    {
      讀文件;
      有讀的權限; 
    }
    catch(UnauthorizedAccessException ex)
    {
      沒有讀的權限;
    }
      

  3.   

    private void Test1()
    {
       StreamReader objReader = new StreamReader("c:\\test.txt");
       string sLine="";
       ArrayList arrText = new ArrayList();
       while (sLine != null)
         {
    try
     {
    sLine = objReader.ReadLine();
    if (sLine != null)
       arrText.Add(sLine);
    }
    catch(UnauthorizedAccessException ex)
    {
    throw ex;
    }
         }
    objReader.Close(); foreach (string sOutput in arrText)
    Response.Write(sOutput);
    }
      

  4.   

    FileStream stream = new FileStream("filePath",FileMode.OpenOrCreate);
    stream.CanRead;
    stream.CanSeek;
    stream.CanWrite;
    试试这个。