Folder Browser Dialog from remote machine perspective like the one SSMS uses

雨燕双飞 提交于 2019-12-24 03:02:03

问题


I'm creating a maintenance tool and I need to let the user to select a file location for new databases. I would use the FolderBrowserDialog except that I want to show the user the directory structure from the point of view of the machine that SQL Server is actually on rather than the client the tool is being run from.

I know SSMS does this whenever you are selecting backup locations, so I'm wondering if the dialog it uses is available or if there's a way to make the FolderBrowserDialog behave in this manner. It seems that when you do it through SMSS you get to bypass certain permissions issues (i.e. the server doesn't need to be shared or anything).

Suggestions?


回答1:


I've been working on a similar task, and came across the same problem. I would happily share the ready solution, but I signed a deal with the devil, and the company owns everything I make while working for them. But the gist of it is this:

  1. Use the Server.EnumAvailableMedia method to get the hard drives on the server.
  2. Call the xp_dirtree stored procedure to get the files and sub-directories in the given path

I ended up making my own file dialog Form using a TreeView. The code that populates it should look something like:

public partial class RemoteFileDialog : Form
{
    public Server server = new Server( new ServerConnection("ServerName", "User", "Password") );

    /* ... */

    public void getServerDrives()
    {
        DataTable d = server.EnumAvailableMedia();
        foreach (DataRow r in d.Rows)
            treeView.Nodes.Add( new TreeNode(r["Name"].ToString() );
    }

    /* ... */

    //populate a node with files and subdirectories when it's expanded
    private void treeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        DataSet ds = server.ConnectionContext.ExecuteWithResults(string.Format("exec xp_dirtree '{0}', 1, 1", e.Node.FullPath));
        ds.Tables[0].DefaultView.Sort = "file ASC"; // list directories first, then files
        DataTable d = ds.Tables[0].DefaultView.ToTable();
        foreach (DataRow r in d.Rows)
            e.Node.Nodes.Add( new TreeNode(r["subdirectory"].ToString());
    }
}


来源:https://stackoverflow.com/questions/20576995/folder-browser-dialog-from-remote-machine-perspective-like-the-one-ssms-uses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!