0

I am trying to get a path to save a file on a remote server, but I don't know how to ensure the path is within a shared directory that I have access to. This is how I was originally trying to do it:

 Dim di As DirectoryInfo = New DirectoryInfo("\\ServerName\")
 Dim diArr As DirectoryInfo() = di.GetDirectories()
 For Each d As DirectoryInfo In diArr....

I was planning on going through the subdirectories of the server until I found one that I can write a file to, but I get this error: "The UNC path should be of the form \server\share". Is there anything I can substitute into the \share part that will exist on every server? Thank you for the help.

Edit: I enumerated the shares on the server using a ManagementObjectSearcher like this:

Dim myObjectSearcher As System.Management.ManagementObjectSearcher
Dim myScope As New ManagementScope("\\" & ServerName & "\root\CIMV2")
Dim myQuery As New SelectQuery("SELECT * FROM Win32_LogicalShareSecuritySetting")

 myObjectSearcher = New System.Management.ManagementObjectSearcher(myScope, myQuery)

For Each share As ManagementBaseObject in myObjectSearcher.Get()
    MessageBox.Show(share.ToString)
Next
cjw
  • 344
  • 1
  • 8
  • 23

1 Answers1

1

\ServerName\IPC$ always exists, but it's not a directory.

\ServerName\C$ will probably always exist, but unless you have administrator credentials you won't be able to read it.

You probably need to enumerate the shares on the server. Take a look at this answer.

Community
  • 1
  • 1
Ross Presser
  • 6,027
  • 1
  • 34
  • 66
  • Thanks for the response. I edited my original post to show you how I am going through all of the shares, but I have one more question: How can I access just the name of the share itself? The share.ToString is returning results like this: \\ServerName\Root\CIMV2:Win32_Folder.Name = "Name".. is there any way I can access just the "Name" part using some function in ManagementBaseObject? – cjw Oct 01 '15 at 19:39
  • `MessageBox.Show(share("Name"))` – Ross Presser Oct 01 '15 at 23:35