按顺序使用数组获取文件

时间:2016-06-05 05:11:52

标签: c# .net arrays winforms file

我正在使用WinForms。在我的表单中,我有一个文本框,我放置一个文件路径来查看该特定文件夹中的文件。问题是我的数组索引命令文件的方式与我在文件夹中看到的不同。如何让我的数组与我在文件夹中看到的相匹配?

    private void Button_Click(object sender, EventArgs e)
    {
      string[] array1 = Directory.GetFiles(img_Source_TxtBox.Text);
    }
  • 请注意,我的数组索引是从该特定目录中随机的。

数组值

enter image description here

我的文件夹值

enter image description here

3 个答案:

答案 0 :(得分:4)

问题是您看到的排序顺序是Windows文件资源管理器会话的一部分,而不是文件在磁盘上“排序”的方式。如您所知,您可以打开两个窗口并进行不同的排序。

为了更接近您的需求,您可以调查:

  • 文件在Windows中的排序方式默认为中性顺序,
  • 如果Windows中的排序算法存在任何差异(例如包含数字的名称)

然后,您必须在应用程序中应用相同的逻辑。

编辑:找到一篇帖子,提供有关此问题的更多详情:Natural Sort Order in C#

答案 1 :(得分:3)

Directory.GetFiles不保证排序顺序。

MSDN说 -

  

无法保证返回文件名的顺序;使用排序   方法,如果需要特定的排序顺序。

这意味着,你必须这样做。我建议使用Linq进行排序。

string[] array1 = Directory.GetFiles(img_Source_TxtBox.Text)
                           .OrderBy(x=>x)
                           .ToArray();

答案 2 :(得分:2)

  public class SendMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send_message);

    final EditText editSMS = (EditText) findViewById(R.id.editText1);
    final EditText editPhoneNum = (EditText) findViewById(R.id.phoneNum);
    Button getSendButton = (Button) findViewById(R.id.button1);