当我尝试使用 C# 保存图像文件时如何修复 "UnauthorizedAccessException"?
How to fix "UnauthorizedAccessException" when I try to save image file with C#?
我正在使用 C# 和 Selenium IWebDriver,我正在尝试拍摄屏幕图像并将文件保存到项目文件夹级别的名为 "Screenshots" 的文件夹中。
我尝试使用 methodSetAccessRule() 设置目录的访问规则
我想尝试 "SaveAsFile"
以外的其他方法
public class Helper
{
public static void TakeScreenshot(string value)
{
IWebDriver driver;
driver = DriverFactory.setDriver("Chrome");
driver.Navigate().GoToUrl(value);
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(Helper.GetScreenShotDirectory(), ScreenshotImageFormat.Png);
}
public static string GetScreenShotDirectory()
{
string cur = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string screenshotDir = Path.Combine(cur, @"..\..\","Screenshots");
SetAccessRule(screenshotDir);
return screenshotDir;
}
public static void SetAccessRule(string directory)
{
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(directory);
FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
}
}
我希望 takeScreenshot 方法中的 SaveAsFile 函数应该将文件保存在 screenshotDir 目录中。
相反,我得到 UnauthorizedAccessException,目录被拒绝
几件事:
- 您只是在更新内存中的
DirectorySecurity
,但并未将权限保存回目录。
例如,查看 Directory.GetAccessControl 文档中的示例:
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
要将更新后的权限写回文件系统,它会调用 DirectoryInfo.SetAccessControl
。你也需要这样做。像这样:
public static void SetAccessRule(string directory)
{
var dInfo = new DirectoryInfo(directory);
System.Security.AccessControl.DirectorySecurity sec = dInfo.GetAccessControl();
FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
dInfo.SetAccessControl(sec);
}
- 如果您运行的帐户没有权限将文件写入目录,它真的有权限更改权限吗? (有可能……只是不太可能)
您得到 System.UnauthorizedAccessException
是因为您试图用 .png 文件覆盖 Screenshots
目录。
// Install-Package Selenium.WebDriver
// Install-Package Selenium.WebDriver.ChromeDriver -Version 75.0.3770.140
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.IO;
using System.Reflection;
using System.Security.AccessControl;
namespace Selenium_Screenshot_SaveAsFile
{
public class DriverFactory
{
public static IWebDriver setDriver(string name)
{
return new ChromeDriver();
}
}
public class Helper
{
public static void TakeScreenshot(string value)
{
IWebDriver driver;
driver = DriverFactory.setDriver("Chrome");
driver.Navigate().GoToUrl(value);
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
// This succeeds because it's saving to a file...
ss.SaveAsFile(Path.Combine(Helper.GetScreenShotDirectory(), "Hello, world!.png"), ScreenshotImageFormat.Png);
// System.UnauthorizedAccessException because it's trying to overwrite a directory...
ss.SaveAsFile(Helper.GetScreenShotDirectory(), ScreenshotImageFormat.Png);
}
public static string GetScreenShotDirectory()
{
string cur = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string screenshotDir = Path.Combine(cur, @"..\..\", "Screenshots");
SetAccessRule(screenshotDir);
return screenshotDir;
}
public static void SetAccessRule(string directory)
{
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(directory);
FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
}
}
class Program
{
static void Main(string[] args)
{
Helper.TakeScreenshot("https://whosebug.com/");
}
}
}
我正在使用 C# 和 Selenium IWebDriver,我正在尝试拍摄屏幕图像并将文件保存到项目文件夹级别的名为 "Screenshots" 的文件夹中。
我尝试使用 methodSetAccessRule() 设置目录的访问规则 我想尝试 "SaveAsFile"
以外的其他方法public class Helper
{
public static void TakeScreenshot(string value)
{
IWebDriver driver;
driver = DriverFactory.setDriver("Chrome");
driver.Navigate().GoToUrl(value);
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(Helper.GetScreenShotDirectory(), ScreenshotImageFormat.Png);
}
public static string GetScreenShotDirectory()
{
string cur = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string screenshotDir = Path.Combine(cur, @"..\..\","Screenshots");
SetAccessRule(screenshotDir);
return screenshotDir;
}
public static void SetAccessRule(string directory)
{
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(directory);
FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
}
}
我希望 takeScreenshot 方法中的 SaveAsFile 函数应该将文件保存在 screenshotDir 目录中。 相反,我得到 UnauthorizedAccessException,目录被拒绝
几件事:
- 您只是在更新内存中的
DirectorySecurity
,但并未将权限保存回目录。
例如,查看 Directory.GetAccessControl 文档中的示例:
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
要将更新后的权限写回文件系统,它会调用 DirectoryInfo.SetAccessControl
。你也需要这样做。像这样:
public static void SetAccessRule(string directory)
{
var dInfo = new DirectoryInfo(directory);
System.Security.AccessControl.DirectorySecurity sec = dInfo.GetAccessControl();
FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
dInfo.SetAccessControl(sec);
}
- 如果您运行的帐户没有权限将文件写入目录,它真的有权限更改权限吗? (有可能……只是不太可能)
您得到 System.UnauthorizedAccessException
是因为您试图用 .png 文件覆盖 Screenshots
目录。
// Install-Package Selenium.WebDriver
// Install-Package Selenium.WebDriver.ChromeDriver -Version 75.0.3770.140
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.IO;
using System.Reflection;
using System.Security.AccessControl;
namespace Selenium_Screenshot_SaveAsFile
{
public class DriverFactory
{
public static IWebDriver setDriver(string name)
{
return new ChromeDriver();
}
}
public class Helper
{
public static void TakeScreenshot(string value)
{
IWebDriver driver;
driver = DriverFactory.setDriver("Chrome");
driver.Navigate().GoToUrl(value);
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
// This succeeds because it's saving to a file...
ss.SaveAsFile(Path.Combine(Helper.GetScreenShotDirectory(), "Hello, world!.png"), ScreenshotImageFormat.Png);
// System.UnauthorizedAccessException because it's trying to overwrite a directory...
ss.SaveAsFile(Helper.GetScreenShotDirectory(), ScreenshotImageFormat.Png);
}
public static string GetScreenShotDirectory()
{
string cur = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string screenshotDir = Path.Combine(cur, @"..\..\", "Screenshots");
SetAccessRule(screenshotDir);
return screenshotDir;
}
public static void SetAccessRule(string directory)
{
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(directory);
FileSystemAccessRule accRule = new FileSystemAccessRule(Environment.UserDomainName + "\" + Environment.UserName, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
}
}
class Program
{
static void Main(string[] args)
{
Helper.TakeScreenshot("https://whosebug.com/");
}
}
}