Silverlight 4 如何取得D盘某个目录文件列表

Kenny田 2010-07-29 09:50:55
我看了jv9(冷秋寒)的两篇文章:
http://blog.csdn.net/jv9/archive/2010/07/23/5756817.aspx
http://www.cnblogs.com/jv9/archive/2010/07/17/1779350.html

http://msdn.microsoft.com/en-us/library/6tkce7xa(v=VS.85).aspx

我看有人问到
http://www.cnblogs.com/jv9/archive/2010/07/17/1779350.html#1873387
jv9说:“如果需要其他目录访问,可以使用COM实现”

我就照着写

using (dynamic fsoCom = AutomationFactory.CreateObject("Scripting.FileSystemObject"))
{
dynamic file = fsoCom.GetFolder(@"D:\Temp");
}

写到这儿,我就不知道该怎么写了,请问如何D盘某个目录文件列表,在线等!
...全文
252 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunpire 2010-07-30
  • 打赏
  • 举报
回复
哈哈,刚想来测试一下有没有权限问题,没想到冷大神就发代码了。

代码不复杂,有用过 VBScript、JavaScript 的 Scripting.FileSystemObject 的朋友应当很熟悉。

另外,由于使用了 DLR ,要引用 Microsoft.CSharp.dll。
jv9 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 kennytian 的回复:]
我看了jv9(冷秋寒)的两篇文章:
http://blog.csdn.net/jv9/archive/2010/07/23/5756817.aspx
http://www.cnblogs.com/jv9/archive/2010/07/17/1779350.html

http://msdn.microsoft.com/en-us/library/6tkce7xa(v=VS.85).as……
[/Quote]

Silverlight在OOB模式下可以使用FileSystemObject获取任何目录下的文件列表,我计划在下一篇例程中展示该功能。

相对你使用的代码稍微复杂一点。你可以参考以下代码:

获取目录


private void GetFolders(Drive currentDrive)
{
dynamic fileSystem = AutomationFactory.CreateObject("Scripting.FileSystemObject");
dynamic folders = fileSystem.GetFolder(string.Format(@"{0}:\",
currentDrive.DriveLetter)).SubFolders;

GetFolders(folders);
}

private void GetFolders(DriveContent currentSelection)
{
dynamic fileSystem = AutomationFactory.CreateObject("Scripting.FileSystemObject");
dynamic folders = fileSystem.GetFolder(string.Format(@"{0}",
currentSelection.Path)).SubFolders;

GetFolders(folders);
}

private void GetFolders(dynamic folders)
{
foreach (var folder in folders)
{
try
{
DriveContents.Add(new DriveContent
{
DriveContentName = folder.Name,
Path = folder.Path,
DriveContentType = "Folder"
});
}
catch (System.Exception) { }
}
}



获取文件:



private void GetFiles(DriveContent currentSelection)
{
dynamic fileSystem = AutomationFactory.CreateObject("Scripting.FileSystemObject");
dynamic files = fileSystem.GetFolder(string.Format(@"{0}",
currentSelection.Path)).Files;

GetFiles(files);
}

private void GetFiles(dynamic files)
{
foreach (var file in files)
{
try
{
DriveContents.Add(new DriveContent
{
DriveContentName = file.Name,
DriveContentType = "File"
});
}
catch (System.Exception) { }
}
}




其中DriveContents和DriveContent类是定义驱动器成员类。

最近两天我把文章发出来。
Sunpire 2010-07-29
  • 打赏
  • 举报
回复
COM 应当没有访问任意目录的权限吧,在OOB中提升了权限也不行。

8,739

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧