"The name 'Drive' does not exist in it's current form" - 以及其他一些问题 C#



这里有几个错误,我需要向我解释。以下是我的代码,我在一些代码旁边添加了带有错误编号的注释:

namespace JamSnaps
{
    class Functions
    {
         public void GetConnectedDevices(ref string[] ConnectedDrives) {
            //Put all connected drives into an array
            System.IO.DriveInfo[] myDrives = System.IO.DriveInfo.GetDrives();
            foreach (System.IO.DriveInfo info in myDrives)
            {
                if (info.DriveType == System.IO.DriveType.Removable && info.IsReady)
                {
                    //textBox1.Text += info.Name + info.VolumeLabel + "rn";
                    //Create array with connected drives and information
                    string[] Drives = new string[] 
                    {
                        info.Name,
                        info.VolumeLabel,
                        info.TotalFreeSpace.ToString(),
                        info.TotalSize.ToString()
                    };
                    //[0] Drive Letter
                    //[1] Drive Name
                    //[2] Free Space
                    //[3] Total Size
                }
            }
            if (Drives.Count > 0)  //1
            {
                ConnectedDrives = Drive; //2
            }
            else 
            {
                ConnectedDrives = false; //3
            }
        }
    }
}
  1. 名称'Drives'在当前上下文中不存在
  2. 名称'Drives'在当前上下文中不存在
  3. 不能隐式地将类型"bool"转换为"string[]"

一个按钮上的更多错误,我试图调用函数:

private void button1_Click(object sender, EventArgs e)
        {
            string[] drives;
            JamSnaps.Functions.GetConnectedDevices(ref drives); //4
            MessageBox.Show(drives);
        }

4:最佳的重载方法匹配jamsnap . functions。GetConnectedDevices(ref string[])'有一些无效的参数

还有更多,但我们先把这些修好。

编辑

现在有意义了,谢谢Cyral,还有一个问题,我在这里做错了什么?

private void button1_Click(object sender, EventArgs e)
        {
            string[] ConnectedDrives;
            JamSnaps.Functions.GetConnectedDevices(ref ConnectedDrives);
        }

我定义数组准备好了,那么ref应该用新数据替换该数组吗?

我没有得到这个范围的东西,在PHP中,我将能够使用[icode]驱动器[/icode]从它外面的IF语句(如果IF语句当然被执行)

不能访问Drives,因为它在不同的作用域中。我想你是在找myDrives代替。

ConnectedDrives是一个字符串数组,因此不能保存布尔值。在这种情况下,如果驱动器计数为0,则应该相应地处理异常。(或将其设置为null)

对于最后的错误,确保用= new string[0]初始化string[] drives

然而…

我认为您正在尝试获得所有驱动器的列表,在这种情况下,只有一个驱动器,因为您的代码在每次迭代中重新创建Drives数组。

您应该将每个符合您的标准的驱动器添加到列表中,并返回该列表。

public void GetConnectedDevices(ref System.IO.DriveInfo[] ConnectedDrives)
{
    //Put all connected drives into an array
    System.IO.DriveInfo[] myDrives = System.IO.DriveInfo.GetDrives();
    List<System.IO.DriveInfo> Drives = new List<System.IO.DriveInfo>();
    foreach (System.IO.DriveInfo info in myDrives)
    {
        if (info.DriveType == System.IO.DriveType.Removable && info.IsReady)
        {
            Drives.Add(info);
        }
    }
    if (Drives.Count > 0)  //1
    {
        ConnectedDrives = Drives.ToArray<System.IO.DriveInfo>();
    }
}

编辑:我不能告诉你确切的原因,但在c#中,你需要在使用ref传递变量之前初始化它们。在本例中,使用上面的代码,使用:

List<System.IO.DriveInfo> ConnectedDrives = new List<System.IO.DriveInfo>();
JamSnaps.Functions.GetConnectedDevices(ref ConnectedDrives);

你真的不需要使用ref,你可以简单地让你的方法返回List<System.IO.DriveInfo>而不是void,因为你不需要传递任何东西给方法:

public List<System.IO.DriveInfo> GetConnectedDevices()
...
if (Drives.Count > 0)  //1
{
    return Drives;
}
在你的button方法中:
List<System.IO.DriveInfo> ConnectedDrives = JamSnaps.Functions.GetConnectedDevices();

最新更新