使用c# (Winform Application)在服务器上创建目录(与您的PC在同一局域网上)



我尝试(从Windows 11)在我的局域网上的Windows 2016服务器上创建一个目录。目录"档案"及其子目录"BN"已经存在,并已授予"每个人"。完全读写权限。但是当我尝试在&;bn&;下创建子文件夹时使用命令

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

我一直得到异常错误"用户名或密码不正确";我如何在上面的代码中包含服务器的用户登录凭据,或者是否有其他方法从c#甚至使用命令提示符在服务器中创建文件夹(在尝试mkdir \WIN-FM8PGNEC4OFArchivesBNA1后得到相同的错误)?

您需要添加网络凭据

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-FM8PGNEC4OFArchivesBNA1");

最新更新