Skip to main content
 首页 » 编程设计

c#之无法创建类的实例

2024年02月23日16zdz8207

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 Users 或 Public 目录,因为它适用于此类操作。