系统未经授权访问异常:对路径 c: 的访问被拒绝

本文关键字:访问 拒绝 路径 授权 异常 系统 | 更新日期: 2023-09-27 18:34:05

我正在向数据库上传和检索文档。上传效果很好但是检索是导致错误的原因这是用于检索的代码

DataGridViewRow row = dataGridView1.CurrentRow;
            int SelectedplantId = Convert.ToInt32(row.Cells["PId"].Value);
            SQLiteConnection conn = new SQLiteConnection(connString);
            conn.Open();
            string qry = "select Otherdocuments from Plant where PId = @PId";
            SQLiteCommand cmd = new SQLiteCommand(qry, conn);
            cmd.Parameters.AddWithValue("@PId", SelectedpId);
            byte[] buffer = (byte[])cmd.ExecuteScalar();
            conn.Close();
            //string filename = Application.StartupPath + "''" + sFileName;
           string SaveLocation = "C:" + "''" + sFileName;
           FileStream fs = null;
           try
           {
               fs = new FileStream(SaveLocation,FileMode.Create);
               fs.Write(buffer, 0, buffer.Length);
           }
           finally
           {
               if (fs != null)
               {
                   ((IDisposable)fs).Dispose();
               }
           }

at fs = new FileStream(SaveLocation,FileMode.Create); 是发生此异常的位置。

Stack Trace
   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)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode)
   at RVEST.frmPlant.btnRetrieveDocs_Click(Object sender, EventArgs e) in C:'D_Drive_Stuff'RVESTV2'RVEST'frmPlant.cs:line 169
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at RVEST.Program.Main() in C:'D_Drive_Stuff'RVESTV2'RVEST'Program.cs:line 26
   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.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
Thank you

太阳

系统未经授权访问异常:对路径 c: 的访问被拒绝

错误消息似乎很明显,该应用程序无权以C:'写入,也可能没有Application.StartupPath

 //string filename = Application.StartupPath + "''" + sFileName;
 //string SaveLocation = "C:" + "''" + sFileName;
 string SaveLocation = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), sFileName);

还有其他一些可能适用的SpecialFolder