[英]Get All current file names in directory and write it out in console C#
I'm writing a program that requires the ability to list all applications installed, and it will do that by listing all the uninstallers for it in a specific directory. 我正在编写一个程序,该程序需要能够列出所有已安装的应用程序,并且它将通过在特定目录中列出所有卸载程序来实现此目的。 This code isn't working, and the directory is created and there are files in the directory
该代码不起作用,并且目录已创建,并且目录中有文件
if (startarg.Contains("-il") == true)
{
//Lists all installed programs here
DirectoryInfo uninstalldir = new DirectoryInfo("Uninstallers");
FileInfo[] UninstallerFiles = uninstalldir.GetFiles();
Console.WriteLine("Listing all applications installed with Simtho");
foreach (FileInfo files in UninstallerFiles)
{
Console.WriteLine(files.Name.ToString());
}
I know it needs the full path, but I won't know the full path so it needs to be a variable, how can I make something like this work? 我知道它需要完整的路径,但是我不知道完整的路径,因此它需要是一个变量,我该如何做这样的工作?
DirectoryInfo uninstalldir = new DirectoryInfo(Directory.GetCurrentDirectory + "\" + "Uninstallers");
我相信您“ DirectoryInfo”对象需要完整路径才能找到卸载程序文件夹,例如:“ C:....”
My bet is that for some reason you are passing a wrong path to the DirectoryInfo constructor after all. 我敢打赌,由于某种原因,您毕竟会将错误的路径传递给DirectoryInfo构造函数。 In order to debug, I would obtain the full path of the dirctory using the following code, and check that it actually refers to the desired path:
为了进行调试,我将使用以下代码获取目录的完整路径,并检查其是否实际指向所需路径:
Path.Combine(Directory.GetCurrentDirectory(), "Uninstallers");
This code seems to be working ,please use this: 该代码似乎有效,请使用以下代码:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.getAllFiles(@"D:\Old_Desktop");
}
public void getAllFiles(string directoryPath)
{
DirectoryInfo dirInfo= new DirectoryInfo(directoryPath);
FileInfo[] files= dirInfo.GetFiles();
foreach(FileInfo f in files)
{
Console.WriteLine(f.FullName);
}
Console.ReadLine();
}
}
And yes the directory is to be given as full path as mentioned by earlier answers and in my code snippet. 是的,如先前的答案和我的代码片段所述,该目录应作为完整路径给出。
尝试
DirectoryInfo uninstalldir = new DirectoryInfo(Path.GetFullPath("Uninstallers"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.