File.open()得到一个异常“”;所需的特权不由客户端持有”;

本文关键字:特权 客户端 open 异常 一个 File | 更新日期: 2023-09-27 18:19:42

以下代码来自教科书:

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: 

File.open()得到一个异常“”;所需的特权不由客户端持有”;

尝试使用不同的路径。看起来你没有对c:的写入权限,这很正常。试试c:''temp之类的东西。