Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 获取所有子文件夹及其大小_C# 4.0_Combobox - Fatal编程技术网

C# 4.0 获取所有子文件夹及其大小

C# 4.0 获取所有子文件夹及其大小,c#-4.0,combobox,C# 4.0,Combobox,我正在尝试写一个方法 public List<string> getList() { string[] str; string no, name, size, price; string albumFolder = @"F:\Audio"; char a = ' '; List<string> albums = new List<string>(); albumFolder.Split(Path.Direct

我正在尝试写一个方法

public List<string> getList()
{
    string[] str;
    string no, name, size, price;
    string albumFolder = @"F:\Audio";
    char a = ' ';

    List<string> albums = new List<string>();

    albumFolder.Split(Path.DirectorySeparatorChar);
    str = albumFolder.Split(Path.DirectorySeparatorChar);

        for (int i = 0; i < str.Length; i++)
        {
            string n = str[i].ToString();
            n = n.Split(Path.DirectorySeparatorChar).ToString();
            no = (i > 8 ? "  " : "    ") + (i + 1) + "".PadRight(10, a);
            name = n.PadRight((155 - n.Length), a);
            size = "" + 512 + " MB".PadRight(20, a); // also help me finding their size
            price = "" + 80 + "".PadRight(10, a);
            albums.Add(no + name + size + price);
        }
    return albums;
}
列表
应包含固定长度的字符串以及所有子文件夹的名称(不是位置,只是名称)。但它就像图片一样:

提前感谢…

更改为

publicbindinglist getList()
{
...
返回新的绑定列表(相册);
}
更改为

publicbindinglist getList()
{
...
返回新的绑定列表(相册);
}

您的代码非常复杂,不需要修改。请使用目录和路径类,即Path.GetDirectoryName,不要手动解析

以下是一些代码,用于以NET4之前的方式遍历文件:

/// <summary>
/// Walks all file names that match the pattern in a directory
/// </summary>
public static IEnumerable<string> AllFileNamesThatMatch(this string fromFolder, string pattern, bool recurse)
{
  return Directory.GetFiles(fromFolder,
                     pattern,
                     recurse ? SearchOption.AllDirectories :
                               SearchOption.TopDirectoryOnly);
}

/// <summary>
/// Walks all file names in a directory
/// </summary>
public static IEnumerable<string> AllFileNames(this string fromFolder, bool recurse)
{
  return fromFolder.AllFileNamesThatMatch("*.*", recurse);
}
//
///遍历目录中与模式匹配的所有文件名
/// 
公共静态IEnumerable AllFileNamesThatMatch(此字符串来自文件夹、字符串模式、布尔递归)
{
返回目录.GetFiles(fromFolder,
图案
recurse?SearchOption.AllDirectory:
SearchOption.TopDirectoryOnly);
}
/// 
///遍历目录中的所有文件名
/// 
公共静态IEnumerable所有文件名(此字符串来自文件夹,bool recurse)
{
从文件夹返回。所有匹配的文件名(“*.*”,递归);
}

您可以通过使用Sum LINQ遍历IEnumerable来获得大小。您的代码非常复杂,不必如此。请使用目录和路径类,即Path.GetDirectoryName,不要手动解析

以下是一些代码,用于以NET4之前的方式遍历文件:

/// <summary>
/// Walks all file names that match the pattern in a directory
/// </summary>
public static IEnumerable<string> AllFileNamesThatMatch(this string fromFolder, string pattern, bool recurse)
{
  return Directory.GetFiles(fromFolder,
                     pattern,
                     recurse ? SearchOption.AllDirectories :
                               SearchOption.TopDirectoryOnly);
}

/// <summary>
/// Walks all file names in a directory
/// </summary>
public static IEnumerable<string> AllFileNames(this string fromFolder, bool recurse)
{
  return fromFolder.AllFileNamesThatMatch("*.*", recurse);
}
//
///遍历目录中与模式匹配的所有文件名
/// 
公共静态IEnumerable AllFileNamesThatMatch(此字符串来自文件夹、字符串模式、布尔递归)
{
返回目录.GetFiles(fromFolder,
图案
recurse?SearchOption.AllDirectory:
SearchOption.TopDirectoryOnly);
}
/// 
///遍历目录中的所有文件名
/// 
公共静态IEnumerable所有文件名(此字符串来自文件夹,bool recurse)
{
从文件夹返回。所有匹配的文件名(“*.*”,递归);
}

您可以通过使用Sum LINQ遍历IEnumerable来获得大小,我认为这就是您可能想要的(尽管我同意有更好/更简单的方法):

public List getList()
{
字符串编号、名称、大小、价格;
字符串albumFolder=@“F:\Audio”;
字符a=“”;
列表相册=新列表();
字符串[]str=Directory.GetDirectories(albumFolder);
对于(int i=0;i8?)+(i+1)+。PadRight(10,a);
name=info.name.PadRight(155,a);
size=”“+512+“MB”.PadRight(20,a);//还可以帮助我找到它们的大小
价格=”+80+”。PadRight(10,a);
相册。添加(编号+名称+大小+价格);
}
归还相册;
}

我认为这正是您想要的(尽管我同意有更好/更简单的方法):

public List getList()
{
字符串编号、名称、大小、价格;
字符串albumFolder=@“F:\Audio”;
字符a=“”;
列表相册=新列表();
字符串[]str=Directory.GetDirectories(albumFolder);
对于(int i=0;i8?)+(i+1)+。PadRight(10,a);
name=info.name.PadRight(155,a);
size=”“+512+“MB”.PadRight(20,a);//还可以帮助我找到它们的大小
价格=”+80+”。PadRight(10,a);
相册。添加(编号+名称+大小+价格);
}
归还相册;
}

从代码中删除下面的行,所有工作正常

n = n.Split(Path.DirectorySeparatorChar).ToString();

从代码中删除下面的行,所有工作正常

n = n.Split(Path.DirectorySeparatorChar).ToString();

是的,这是。但是大小和价格列没有正确对齐。您好,布莱恩,是的,您需要使用单字字体/固定宽度字体来实现您想要的外观。此外,我还修复了“name”字段周围答案中的一个小错误——您不想从填充中减去长度。祝你好运是的,这是。但是大小和价格列没有正确对齐。您好,布莱恩,是的,您需要使用单字字体/固定宽度字体来实现您想要的外观。此外,我还修复了“name”字段周围答案中的一个小错误——您不想从填充中减去长度。祝你好运
n = n.Split(Path.DirectorySeparatorChar).ToString();