关键词搜索

源码搜索 ×
×

C# 写成ActiveX空间后对目录操作没有权限

发布2018-03-27浏览675次

详情内容

在使用截图和录像的过程中遇到目录创建不成功的情况。

输出的BUG日志
System.UnauthorizedAccessException: 对路径“D:\Program Files (x86)\CvNavi\CvVideoOcx\record”的访问被拒绝。
   在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   在 System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
   在 System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
   在 System.IO.Directory.CreateDirectory(String path)
   在 CvNetVideo.Utils.NVCheji.StartRecord() 位置 D:\DEVELOPERS\vs_workspace\SQWL\RealTimeVideo\CvNetVideo\Utils\NVCheji.cs:行号 62
   在 CvNetVideo.UCVideo.mbtnRecord_Click(Object sender, EventArgs e) 位置 D:\DEVELOPERS\vs_workspace\SQWL\RealTimeVideo\CvNetVideo\UCVideo.cs:行号 194
   在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
   在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
   在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   在 System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   在 System.Windows.Forms.ToolStrip.WndProc(Message& m)
   在 System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

使用系统拥有权限的目录

C:\\ProgramData\\

C:\\Documents and Settings\\All Users\\Application Data\\

  1. var baseDir = "C:\\ProgramData\\screenshot\\";
  2. //var baseDir = "C:\\Documents and Settings\\All Users\\Application Data\\screenshot\\";
  3. // 创建截图保存目录
  4. Directory.CreateDirectory(baseDir);
使用管理员运行代码授权
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.AccessControl;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CvNetVideo.Play
  9. {
  10. /// <summary>
  11. /// 系统路径和文件授权辅助类
  12. /// </summary>
  13. public class AuthorizationHelper
  14. {
  15. /// <summary>
  16. /// 为文件添加users,everyone用户组的完全控制权限
  17. /// </summary>
  18. /// <param name="filePath"></param>
  19. public static void AddSecurityControll2File(string filePath)
  20. {
  21. try
  22. {
  23. //获取文件信息
  24. FileInfo fileInfo = new FileInfo(filePath);
  25. //获得该文件的访问权限
  26. System.Security.AccessControl.FileSecurity fileSecurity = fileInfo.GetAccessControl();
  27. //添加ereryone用户组的访问权限规则 完全控制权限
  28. fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
  29. //添加Users用户组的访问权限规则 完全控制权限
  30. fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
  31. //设置访问权限
  32. fileInfo.SetAccessControl(fileSecurity);
  33. }
  34. catch (Exception ex)
  35. {
  36. throw ex;
  37. }
  38. finally
  39. {
  40. Console.WriteLine("AddSecurityControll2File is executed.");
  41. }
  42. }
  43. /// <summary>
  44. ///为文件夹添加users,everyone用户组的完全控制权限
  45. /// </summary>
  46. /// <param name="dirPath"></param>
  47. public static void AddSecurityControll2Folder(string dirPath)
  48. {
  49. try
  50. {
  51. //获取文件夹信息
  52. DirectoryInfo dir = new DirectoryInfo(dirPath);
  53. //获得该文件夹的所有访问权限
  54. System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
  55. //设定文件ACL继承
  56. InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
  57. //添加ereryone用户组的访问权限规则 完全控制权限
  58. FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
  59. //添加Users用户组的访问权限规则 完全控制权限
  60. FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
  61. bool isModified = false;
  62. dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);
  63. dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified);
  64. //设置访问权限
  65. dir.SetAccessControl(dirSecurity);
  66. }
  67. catch (Exception ex)
  68. {
  69. throw ex;
  70. }
  71. finally
  72. {
  73. Console.WriteLine("AddSecurityControll2Folder is executed.");
  74. }
  75. }
  76. }
  77. }

注:此种方法必须以管理员方式运行才能拥有权限。

参考文章:https://www.cnblogs.com/wolf-sun/p/4591734.html

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载