我有一个 C# 进程需要读取远程服务器上共享目录中存在的文件。

以下代码导致“共享不存在”。正在写入控制台。

string fileName = "someFile.ars";
string fileLocation = @"\\computerName\share\";
if (Directory.Exists(fileLocation))
{
    Console.WriteLine("Share exists.");
}
else
{
    Console.WriteLine("Share does not exist.");
}

该过程在 AD 用户帐户下运行,并且该帐户被授予对共享目录的完全控制权限。我可以成功地将共享映射为进程所在机器上的网络驱动器,并且可以将文件复制到/从目录中复制。关于我缺少什么的任何想法?

最佳答案

使用 File.Exists 而不是 Directory.Exists

另外,您可能希望与平台无关,并像这样使用规范的 Path.Combine:

string fileName = "someFile.ars";
string fileServer = @"\\computerName";
string fileLocation = @"share";
if (File.Exists(Path.Combine(fileServer, fileLocation, fileName)))
{
    Console.WriteLine("Share exists.");
}
else
{
    Console.WriteLine("Share does not exist.");
}

关于c# - UNC 共享目录不存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10841149/

10-13 06:08