0

(known) Issue:

I have a specific application part in order to create a user home drive on a network location:

System.Net.NetworkCredential cred = new System.Net.NetworkCredential(myadmin,pwd);
using (new NetworkConnection(@"\\server\Data\Home", cred))
{
    string path = $@"\\server\Data\Home\{userName}";
    Directory.CreateDirectory(path);
    SetFullPermission(path, userName);
}

In some occurrences, the code above might fail with the following error message:

Multiple connections to a server or shared resource from the same user using multiple usernames are not allowed. Disconnect all previous connections to the server or shared resource, and try...

Cause:

The error is caused if my local user on the machine had the network drive open at any given time (also if the folder is closed again) or if the home drive was mapped at any given time (until computer is rebooted)

Workaround:

  1. Remove home drive from user and remove logon script (so that no drives to the server are automapped)
  2. Reboot the computer and log on
  3. Execute the software
  4. When task is finished, add logon script and home drive to own user again and relogon, in order to have access to network drives

Question:

Is there a way to cancle any given relevant network connection prior to using (new NetworkConnection(@"\\server\Data\Home", cred)) in my c# application?

4
  • Silly question perhaps, but is the user able to connect to the "path" even when there is no reboot? So when the home drive creation gives an error can the user access that network drive? Commented May 27, 2022 at 8:54
  • Maybe do a WNetCancelConnection2 when you hit that exception. Not sure if that call has a managed equivalent, otherwise do a pinvoke. The parameters aren't that tricky. Commented May 27, 2022 at 8:54
  • My local user is able to connect to the path. My admin (which is required to create the folder / assign permissions) is not, if the network location / folder was open at any given time (in windows explorer) only a reboot will release the connection WNetCancelConnection2 unfortunately had no effect but maybe I used it wrong, ill read the Documentation. Commented May 27, 2022 at 9:22
  • If your user is able to access the path, why fail if the path already exists? Commented May 27, 2022 at 9:27

2 Answers 2

0

In the comments I already hint about this, but if your user is able to access the path, why fail? Your intent is to create the username-path-folder. Which is already there and therefor successful or am I mistaken?

        string path = $@"\\server\Data\Home\{userName}";
        try
        {
            System.Net.NetworkCredential cred = new System.Net.NetworkCredential(myadmin, pwd);
            using (new NetworkConnection(@"\\server\Data\Home", cred))
            {
                Directory.CreateDirectory(path);
                SetFullPermission(path, userName);
            }
        }
        catch(SpecificException)
        {
            // you can ignore this one - you want access to the folder - and you already have it
            // you could try to write and delete a temp file to ensure access as a failsafe
        }
        catch(Exception)
        {
            throw;
        }

Something like this perhaps? You have a folder (which the admin created) so, there is no need to fail.

Sign up to request clarification or add additional context in comments.

1 Comment

as administrator, i need to create the folder $@"\\server\Data\Home\{userName}" and need to apply permissions for {userName}. The error message arises when I accessed any folder on {server} with my own (non administrator) user account before.
0

add a new DNS alias for "Server" either in the local hosts file,or on the DNS server in a server envionment.

for me, i have a "Server_Write" alias, for a server that general users (including me) access to only read data, but apps can then be created that access the "Server_Write\Data" share using other credentials that have permission to save new files or create folders as you are doing. this gave me the idea... https://backupchain.com/i/how-to-fix-error-1219-multiple-connections-to-a-server-or-shared-resource-by-the-same-user

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.