Yassir Yahaya
Yassir Yahaya

Reputation: 13

Creating a Directory on a Server (On the same LAN with your PC) using C# (Winform Application)

I tried (from Windows 11) to create a Directory on a Windows 2016 Server sitting on my LAN. The Directory "Archives" and its subdirectory "BN" are already existing and had granted "EVERYONE" Full Permission to read/write in them. But when I tried to create a subfolder under "BN" using the command

try
 {
    Directory.CreateDirectory(@"\\WIN-FM8PGNEC4OF\Archives\BN\A1");
 }
 catch (Exception ex)
 {
    MessageBox.Show(ex.Message);
 }

i kept getting the exception error "The user name or password is incorrect." How do I include the Server's user login credentials in the code above, or are there other ways of creating a folder in a server from a C# or even using Command Prompt (gets the same error after trying mkdir \\WIN-FM8PGNEC4OF\Archives\BN\A1) ?

Upvotes: 1

Views: 393

Answers (1)

CorrieJanse
CorrieJanse

Reputation: 2598

You need to add Network credentials

NetworkCredential theNetworkCredential = new NetworkCredential(username, password, domain);
CredentialCache theNetcache = new CredentialCache();
theNetCache.Add(@"\\computer", theNetworkCredential, "Basic", theNetworkCredential);
//then do whatever, such as creating folders:
Directory.CreateDirectory(@"\\WIN-FM8PGNEC4OF\Archives\BN\A1");

Upvotes: 1

Related Questions