title: Check if server path is available as file share in C#
body: <p>I want to quickly check if a file share is available in C#, but without knowing anything about the directories that might be on the network share.  I have found these posts <a href="https://stackoverflow.com/questions/5152647/how-to-quickly-check-if-unc-path-is-available">1</a> <a href="https://stackoverflow.com/questions/7963149/fastest-way-to-identify-network-path-exists">2</a> <a href="https://stackoverflow.com/questions/4027767/how-to-determine-if-a-network-path-is-available-or-not-online-or-offline">3</a> that show how to check if a network directory is available, but they all assume I know the directory share that I want to check if exists. That is, they want to check if <strong>\\SomeServer\SomeDirectory</strong> is available, but I just want to check if <strong>\\SomeServer</strong> is available.</p>
  
  <p>For a little more detail about what I'm trying to do, I prompt the user for a SQL server to connect to, and they give me an address such as "SQL001"; obviously this address is only valid when on our internal network. With this address I'm able to connect to the server and its databases. Now, I give them the option to backup a database, and want the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx" rel="nofollow noreferrer">OpenFileDialog</a> to have the InitialDirectory set to "\\SQL001" so they can quickly access a shared folder on that server and back the database up on the remote server.</p>
  
  <p>If I set "\\SQL001" as the OpenFileDialog's InitialDirectory, everything works fine, however if they mistyped and put "\\SQL002" (which doesn't exist), or maybe try and use the tool when off the internal network, then the OpenFileDialog's ShowDialog function throws an error. So I want to check and make sure that the file share is available first, and if not I won't set the InitialDirectory.</p>
  
  <p>Using <strong>Directory.Exists("\\SQL001")</strong> always returns false unfortunately. If I do <strong>Directory.Exists("\\SQL001\Backups")</strong> it works, but we have many different SQL servers and they don't all have a share called "Backups", so that is not reliable. I can also use <strong>Directory.Exists("\\SQL001\c$\")</strong> which works for me, but many employees will not have permissions to the root C:\, but will have permissions for the network shares, so that is not a good alternative either.</p>
  
  <p>So my question is, assuming the user has permissions to a file share, how can I check that the file share is available? Also, I don't want to force the user to <a href="https://stackoverflow.com/questions/676734/net-driveinfo-with-unc-paths">map "\\SQL001" as a network drive</a> either.</p>
  
  <p>The only solution I can see right now is to just call the OpenFileDialog's ShowDialog function and catch the specific exception, wipe out the InitialDirectory and then call ShowDialog again. It'll work, but feels a bit hacky, so I'm hoping for a better solution.</p>