1
votes

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];
}

TestClass.cs:

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

Why am I getting this error? I made sure that the folder is shared to everyone, and removed the Read Only.

3

3 Answers

2
votes

You're getting this error because as the exception message says the current user doesn't have access to the file in question. You need to adjust the permissions of that file and it's parent directories to allow access to the user in question

Part of the problem is your choice of location. It looks like you are attempting to share a configuration file between a set of users yet you've chosen the location of the document to be under the Administrator directory.

C:\Documents and Settings\Administrator\My Document

If you want to share amongst users then I would use the All Users or Public directory instead as it's meant for this type of operation.

1
votes

When you share a folder, Windows looks at two sets of permissions: the Sharing permissions, then the actual file security permissions. Unless your folder security is set to allow everyone read access, it doesn't matter what your sharing settings are set to.

The best practice is to set the Sharing Level to Full Control, then fine tune the permissions using the Security settings. By default, your user folder (Administrator) is writeable and readable only by that named user and the admins on the system.

1
votes

When it says it doesn't have permissions it's probably that - If you're running web site for example, you can't read any folder because your default user that gets used for anonymous visitors is 'sandboxed' to low permissions on file system.

Double check under which username your application is running (web site or desktop app), set both security and sharing permissions for it (if you're accessing it via share).

One other error is that the file doesn't exist. That's one of the strange misleading errors we had - but, to be sure, check the value of sConfigFile just before it executes.