c# - 无法创建类的实例

标签 c# .net exception selenium

Unable to create instance of class TestClass. Error: System.UnauthorizedAccessException: Access to the path 'C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject' is denied.

System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 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) System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) System.IO.FileStream..ctor(String path, FileMode mode) KM_Automation.KM_Library.GetAutoConfig() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\Test_Library.cs: line 40 KM_Automation.KM_Functional_Trans_General..ctor() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestClass.cs: line 33

Test_Library.cs:

public string[] GetAutoConfig()
{
    FileStream fs = new FileStream(sConfigFile, FileMode.Open);  // line 40
    StreamReader sr = new StreamReader(fs);
    string line = "";
    string[] arrline = new string[2];
}

测试类.cs:

arrConfig = KMLib.GetAutoConfig(); // line 33

为什么会出现此错误?我确保该文件夹与所有人共享,并删除了只读。

最佳答案

您收到此错误是因为异常消息表明当前用户无权访问相关文件。您需要调整该文件及其父目录的权限,以允许相关用户访问

部分问题在于您对位置的选择。看起来您正在尝试在一组用户之间共享配置文件,但您已将文档的位置选择在 Administrator 目录下。

C:\Documents and Settings\Administrator\My Document

如果您想在用户之间共享,那么我会使用 All UsersPublic 目录,因为它适用于此类操作。

关于c# - 无法创建类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6253323/

相关文章:

c# - StackExchange.Redis 和 ServiceStack.Redis 的区别

.net - 如何在实际绘画之前在光标处显示画笔?

c# - 我如何使用 LINQ 将这个父子对象模型投影到一个平面的单个对象中?

c# - 从不安全字节指针获取字符串到固定字符数组

java - 当我迭代 ArrayList 时出现 ConcurrentModificationException

exception - 只读集合不支持操作

c# - 是否可以向 App_Code 添加两种语言代码文件,即 C# 和 VB.NET?如果是这样,我怎么能做到这一点?

c# - 使用 entlib 进行单元测试 - 不包括捕获

c# - AleaGPU 是否与 CUDA 工具包 8.0 兼容

java.sql.SQLException : ORA-00902: invalid datatype 异常