C#get文件不起作用 [英] C# get files not working

查看:78
本文介绍了C#get文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道为什么这只是几个.txt文件并没有输出任何预期。



我尝试过:



No idea why this isnt working its just a couple of .txt files and no expection is being outputted.

What I have tried:

try
      {
          string[] array2 = Directory.GetFiles(@"C:\Users\Public\LSH\SearchingDB\", "*.txt");
          if (array2.Length == 0)
          {
              Console.WriteLine("No files found.");
          }
          foreach (string name in array2)
          {
              if (name.Contains(TextBox1.Text))
              {
                  Console.WriteLine(name);
              }
          }
      }
      catch (Exception excpt)
      {
          Console.WriteLine(excpt);
      }

推荐答案

嗯......你从来没有说过什么不行。这不是一个正确的问题描述。



GetFiles工作正常。我要检查的第一件事是你给它的路径。路径不存在,或者您没有权限,或者该文件夹中没有扩展名为txt的文件。



接下来,在调试器中运行它,设置断点,逐步执行代码并检查变量的内容以查看其中的内容。现在,你只是猜测你的代码在做什么。



其他一些愚蠢的事情是Windows Forms应用程序中没有控制台。 Console方法的任何输出都会输出到Visual Studio中的输出窗口。这实际上应该由Debug.WriteLine替换。
Ummm...You never said what "isn't working". That's not a proper problem description.

GetFiles works just fine. The first thing I'd check is the path you gave it. Either the path doesn't exist, or you don't have permissions to it, or there are no files with the extension of "txt" in that folder.

Next, run this in the debugger, set breakpoints, step through the code and inspect the contents of variables to see what in them. Right now, you're just guessing at what you're code is doing.

Something else goofy is that there is no Console in a Windows Forms app. Any output from the Console methods get output to the Output window in Visual Studio. This should really be replaced by Debug.WriteLine instead.


这篇关于C#get文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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