无法将'system.web.ui.webcontrols.listitem'类型转换为'system.io.driveinfo' [英] Cannot convert type 'system.web.ui.webcontrols.listitem' to 'system.io.driveinfo'

查看:50
本文介绍了无法将'system.web.ui.webcontrols.listitem'类型转换为'system.io.driveinfo'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DriveInfo drive =(DriveInfo)lstdrive.SelectedItem;
foreach (DriveInfo diInfo in drive.RootDirectory.GetDirectories())
{
    lstfolder.Items.Add(diInfo.ToString());
}





我的尝试:



DriveInfo drive =(DriveInfo)lstdrive.SelectedItem.tostring();



What I have tried:

DriveInfo drive =(DriveInfo)lstdrive.SelectedItem.tostring();

推荐答案

嗯...看看你在做什么:

Um...look at what you are doing:
foreach (DriveInfo diInfo in drive.RootDirectory.GetDirectories())
{
    lstfolder.Items.Add(diInfo.ToString());
}

因此,您添加到ListView的每个项目都是字符串,而不是DriveInfo。

所以当您尝试将其强制转换为driveinfo时:

So each item you add to the ListView is a string, not a DriveInfo.
So when you try to cast it back to a driveinfo:

DriveInfo drive =(DriveInfo)lstdrive.SelectedItem.tostring();

你有很多问题:

首先,在SelectedItem上使用ToString将其转换为字符串,因此无论ListView包含什么类型,它都不能转换为除字符串或从字符串派生的类之外的任何类型 - 因为string是密封类没有什么可以从中得到。所以这段代码:

You have a number of problems:
First, the use of ToString on the SelectedItem turns it into a string, so regardless of what type the ListView contained, it can't be cast to anything except a string or a class derived from a string - and since string is a sealed class nothing can be derived from it. So this code:

AnyClassOtherThanStringOrObject x = (AnyClassOtherThanStringOrObject) anyValue.ToString();

总是会失败。



其次,由于在将其插入列表视图时将其转换为字符串,如果从SelectedItem中删除ToString调用,则仍会出现完全相同的问题 - 就像项目值一样,是一个字符串的实例!



您可能需要做的是将DriveInfo直接存储到ListView中,并在检索时将其直接转换回DriveInfo SelectedItem ......但是......



第三,这是基于网络的,所以你应该知道在生产中代码很可能仍然无法正常工作! C#代码在服务器而不是客户端上执行,因此它扫描的根目录是服务器根目录,而不是客户端 - 它在测试时似乎工作,因为它们是同一台机器。但在生产中,服务器可能距离客户端数千公里,并且暴露服务器磁盘是一个可怕的安全风险!

您的代码无法直接访问客户端硬盘 - 您应该感到高兴。因为如果你能做到,那么恶意网站就可以......

will always fail.

Secondly, since you converted it to a string when you inserted it to the listview, you will still have exactly the same problem if you remove the ToString call from SelectedItem - as what the item value is, is an instance of a string!

Probably what you need to do is store the DriveInfo directly into the ListView, and directly cast it back to a DriveInfo when you retrieve the SelectedItem ... but ...

Thirdly, this is web based, so you should be aware that in production that code is in all probability still not going to work! The C# code executes on the Server, not the Client, so the root directory it scans is the Server root, not the Client - it appears to work when you test it because they are the same machine. But in production, the Server may be several thousand KM from the Client, and exposing the Server disk is a horrible security risk!
Your code cannot access the Client harddrive directly - and you should be glad of that. Because if you could do it, so could malicious sites...


这篇关于无法将'system.web.ui.webcontrols.listitem'类型转换为'system.io.driveinfo'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆