欢迎光临
我们一直在努力

利用DataGrid显示某目录下的所有文件-.NET教程,数据库应用

建站超值云服务器,限时71元/月

.net framework提供两个访问目录信息的类和两个访问文件信息的类,钥访问目录,可以使用directory类,也可以使用directoryinfo类,对应于文件,也有file类和fileinfo类。这两个类之间的区别在于返回的信息和使用的方法不同。directory和file类属于静态类,在使用时你不必创建实例,例如:要删除文件,可以file.delete(filepath),要检测文件夹是否存在可以directory.exists(directorypath)。而带info的类在使用时必须先进行实例化,并在构造函数里指名文件名字或者目录名字,因此,删除文件可以用dim myfile as file = new file(filepath),myfile.delete()。

这两个类都提供了得到一个文件夹下的所有文件或者某种扩展名的文件的方法,即getfiles()。

&#8212; directory 例子 &#8212;-<br /> dim files() as string = directory.getfiles(directorypath[, optionalwildcard])</p> <p> &#8212; directoryinfo 例子 &#8212;-<br /> dim mydir as directoryinfo = new directoryinfo(directorypath)<br /> dim fileinfos() as fileinfo = mydir.getfiles([optionalwildcard])<br />

值得注意的是,两者返回的类型是不同的,一个是string类型的数组,另外一个是fileinfo类型的数组。下面就是绑定的方法:

vb.net

<%@ import namespace="system.io" %><br /> <script language="<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/programme/vb/index.html" target="_blank" rel="nofollow" >vb</a>" runat="server"> sub page_load(sender as object, e as eventargs) dim dirinfo as new directoryinfo(server.mappath("")) articlelist.datasource = dirinfo.getfiles("*.<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>x") articlelist.databind() end sub </script></p> <p><<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:datagrid runat=&#8221;server&#8221; id=&#8221;articlelist&#8221; font-name=&#8221;verdana&#8221;<br /> autogeneratecolumns=&#8221;false&#8221; alternatingitemstyle-backcolor=&#8221;#eeeeee&#8221;<br /> headerstyle-backcolor=&#8221;navy&#8221; headerstyle-forecolor=&#8221;white&#8221;<br /> headerstyle-font-size=&#8221;15pt&#8221; headerstyle-font-bold=&#8221;true&#8221;><br /> <columns> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:hyperlinkcolumn datanavigateurlfield=&#8221;name&#8221; datatextfield=&#8221;name&#8221;<br /> headertext=&#8221;<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/soft/tools/file/index.html" target="_blank" rel="nofollow" >文件</a>名&#8221; /><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:boundcolumn datafield=&#8221;lastwritetime&#8221; headertext=&#8221;最后修改时间&#8221;<br /> itemstyle-horizontalalign=&#8221;center&#8221; dataformatstring=&#8221;{0:d}&#8221; /><br /> <<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:boundcolumn datafield=&#8221;length&#8221; headertext=&#8221;<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/soft/tools/file/index.html" target="_blank" rel="nofollow" >文件</a>大小&#8221;<br /> itemstyle-horizontalalign=&#8221;right&#8221;<br /> dataformatstring=&#8221;{0:#,### 字节}&#8221; /><br /> </columns> </<a href="http://www.west999.com/www/go/?url=http://www.chinaitpower.com/dev/web/asp/index.html" target="_blank" rel="nofollow" >asp</a>:datagrid><br />

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 利用DataGrid显示某目录下的所有文件-.NET教程,数据库应用
分享到: 更多 (0)