JTinkers
New member
- Joined
- Jul 29, 2025
- Messages
- 3
- Programming Experience
- 5-10
Hey there,
I come with a very simple yet intriguing issue and I was wondering if anyone else found a solution to it.
I have the following environment:
- a working server
- a working and shared network drive
- a simple app that uses the network path (via Directory.Exists)
What I've noticed is, when the server is OK, but for some reason the path IS NOT - the `Directory.Exists` method is going to take an awful long time to return `false`.
This is likely due to internals waiting for network to eventually time out, and since this method is called every HTTP request is received - it's delaying all responses by that much (usually 30s).
I've prepared a small repro code, just bear in mind you would need to at least setup a local shared folder:
I come with a very simple yet intriguing issue and I was wondering if anyone else found a solution to it.
I have the following environment:
- a working server
- a working and shared network drive
- a simple app that uses the network path (via Directory.Exists)
What I've noticed is, when the server is OK, but for some reason the path IS NOT - the `Directory.Exists` method is going to take an awful long time to return `false`.
This is likely due to internals waiting for network to eventually time out, and since this method is called every HTTP request is received - it's delaying all responses by that much (usually 30s).
I've prepared a small repro code, just bear in mind you would need to at least setup a local shared folder:
C#:
using System.Diagnostics;
using System.Net.NetworkInformation;
namespace TestNetworkPathsQuick
{
internal class Program
{
static void Main(string[] args)
{
CheckPath(@"\\192.168.1.10\test"); // few ms, happy path
CheckPath(@"\\192.168.1.11\test"); // few ms
CheckPath(@"\\192.168.1.10\wrong"); // 8-30 seconds
}
static void CheckPath(string uncPath)
{
Stopwatch sw = Stopwatch.StartNew();
string server = GetServerFromUncPath(uncPath);
bool exists;
if (PingHost(server))
{
exists = Directory.Exists(uncPath);
}
else
{
exists = false;
Console.WriteLine($"Server {server} unreachable. Skipping Directory.Exists");
}
sw.Stop();
Console.WriteLine($"Dir exists: {uncPath} - {exists}, elapsed: {sw.Elapsed}");
}
static string GetServerFromUncPath(string uncPath)
{
if (uncPath.StartsWith(@"\\"))
{
var parts = uncPath.Substring(2).Split('\\');
if (parts.Length > 0)
return parts[0];
}
return string.Empty;
}
static bool PingHost(string host)
{
try
{
using var ping = new Ping();
var reply = ping.Send(host, 1000); // 1 second timeout
return reply.Status == IPStatus.Success;
}
catch
{
return false;
}
}
}
}