【问题标题】:File.open() got an exception "A required privilege is not held by the client" [duplicate]File.open() 出现异常“客户端未持有所需的权限”[重复]
【发布时间】:2014-10-21 12:32:29
【问题描述】:

以下代码来自教科书:

namespace FileStreamApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with FileStreams *****\n");
            using (FileStream fStream = File.Open(@"C:\myMessage.dat", FileMode.Create))
            {

                string msg = "Hello!";
                byte[] msgAsByteArray = Encoding.Default.GetBytes(msg);

                fStream.Write(msgAsByteArray, 0, msgAsByteArray.Length);
                // Reset internal position of stream. 
                fStream.Position = 0;

                Console.Write("Your message as an array of bytes: ");
                byte[] bytesFromFile = new byte[msgAsByteArray.Length];
                for (int i = 0; i < msgAsByteArray.Length; i++)
                {
                    bytesFromFile[i] = (byte)fStream.ReadByte();
                    Console.Write(bytesFromFile[i]);
                }
                // Display decoded messages. 
                Console.Write("\nDecoded Message: ");
                Console.WriteLine(Encoding.Default.GetString(bytesFromFile));
            }
            Console.ReadLine();
        }
    }
}

但是当我运行它时,我得到了一个运行时错误。我想不通。错误说:

 System.IO.IOException was unhandled
 HResult=-2147023582
 Message=A required privilege is not held by the client.

 Source=mscorlib
 StackTrace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   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, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,   
   FileShare share)
   at System.IO.File.Open(String path, FileMode mode)
   at FileStreamApp.Program.Main(String[] args) in   
   C:\MyVSExamples\FileStreamApp\Program.cs:line 15
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, 
   String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, 
   ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, 
   ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, 
   ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

   InnerException: 

【问题讨论】:

  • 您需要以管理员身份运行...
  • A required privilege is not held by the client.您没有打开文件路径的适当权限。
  • 来自related questionWhile attempting to create a text file, I got the error "A required privilege is not held by the client." which I googled, has something to do with user's privileges. Simply put, I am not allowed to create a file in my chosen path, which is C:/

标签: c# exception io


【解决方案1】:

尝试使用不同的路径。看起来您没有对 c: 的写权限,这是正常的。改用 c:\temp 之类的东西。

【讨论】:

  • C:\temp 很少存在。
  • @SLaks 你是对的,这就是我说“类似”的原因。
猜你喜欢
  • 1970-01-01
  • 2015-09-08
  • 2020-10-05
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
相关资源
最近更新 更多