我試圖將TreeView控件綁定到我的局域網上的公用文件夾機器ASUS-PC。用於網絡PC的DirectoryInfo拋出ArgumentException
但是,我想不出如何解析「華碩電腦」目錄,因爲我無法從路徑"\\\\ASUS-PC"
創建DirectoryInfo對象。
DirectoryInfo dir = new DirectoryInfo("\\\\ASUS-PC");
的線的上方拋出與消息The UNC path should be of the form \\server\share.
下面的代碼只創建根目錄的啞節點ArgumentException,所以不能顯示所有的公共文件夾的該PC上。
如何獲取路徑「\\ ASUS-PC」的目錄列表?
public void BindDirectoryToTreeView(string directoryPathToBind) {
if (!String.IsNullOrEmpty(directoryPathToBind) && Directory.Exists(directoryPathToBind)) {
treeView1.Nodes.Clear();
TreeNode rootNode = null;
char[] ps = treeView1.PathSeparator.ToCharArray();
string[] split = directoryPathToBind.Split(ps);
var folders = split.Where(str => !String.IsNullOrEmpty(str));
for (int i = 0; i < folders.Count(); i++) {
var folder = folders.ElementAt(i);
int index = directoryPathToBind.IndexOf(folder);
int length = index + folder.Length;
string path = directoryPathToBind.Substring(0, length);
if (rootNode != null) {
DirectoryInfo dir = new DirectoryInfo(path);
TreeNode node = new TreeNode(dir.Name, 0, 0);
RecurseFolders(dir, node);
} else {
//DirectoryInfo dir = new DirectoryInfo(path); <= Throws the Error
rootNode = new TreeNode(folder, 0, 0);
//RecurseFolders(dir, rootNode);
}
}
treeView1.Nodes.Add(rootNode);
if (0 < rootNode.Nodes.Count) {
rootNode.Expand();
}
}
}
我知道這可以做到。那麼,我需要做什麼?