开发者

SMB Directory Listing with C#

I am trying to do a remote directory listing of an SMB web server.

My method however doesn't work. The call to DirectoryInfo() complains that I can't give it 开发者_运维技巧a string in a URi format.

This is what I have come up with:

string smbDirectory = @"\\SERVER\MYSHARE";
Uri requestURI = new Uri(smbDirectory);
List<FileInfo> files = this.listAllFilesInWebDirectory(requestURI);

List<FileInfo> listAllFilesInWebDirectory(Uri smbDirectory)
{
    string filePattern = "*";
    List<FileInfo> returnable = null;
    DirectoryInfo directory = new DirectoryInfo(smbDirectory.AbsoluteUri);

    if (directory.Exists == true)
    {
        FileInfo[] files = directory.GetFiles(filePattern);
        returnable = new List<FileInfo>(files);
    }
    return returnable;
}

I keep getting an exception when I call directory.GetFiles(filePattern).


Looks to me like the DirectoryInfo class only accepts local and UNC file paths, not URLs like http://...


As ewall said, you cannot use uri's.

In order to do this, get rid of the Uri and instead pass the smbDirectory string to your listallfiles function.

But given the edit history of this question that probably doesn't solve the real problem.

--
If you want to enumerate a directory structure of a web server then you are going to have to implement something on the web server itself to support this. For MS servers the "easy" way is to install webdav and leverage it's api to do what you want. However, you could just as easily implement your own web service on the server and query it that way as well.

An alternate route is to configure the web server to allow directory browsing. Make sure you do not have a default document set. Once configured you can use the standard HttpRequest class to make http calls to the server and parse the results.

A third route would be to install an FTP server and just run your commands via FTP.


The point is that the DirectoryInfo class will not work for what you want.


And I'm not convinced that any of the above is really what you want either given some of your other questions regarding silverlight and file access. Maybe you could state exactly what you want your application to do and what problem you are trying to solve. There are many people here who can guide you down the right path so to speak.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜