C#列出没有隐藏文件的文件(加上列出选项)
下面是一个代码,列出我在目录中的文件,然后用户可以键入文件名来打开该文件C#列出没有隐藏文件的文件(加上列出选项),c#,C#,下面是一个代码,列出我在目录中的文件,然后用户可以键入文件名来打开该文件 public static void openFile() { // List files in FormatedDocuments directory String[] showFiles = Directory.GetFiles("FormatedDocuments"); int filesList = showFiles.Get
public static void openFile()
{
// List files in FormatedDocuments directory
String[] showFiles = Directory.GetFiles("FormatedDocuments");
int filesList = showFiles.GetUpperBound (0) + 1;
const String folderToOpen = @"FormatedDocuments/";
Console.WriteLine ("Here is the list of files:");
for (int i = 0; i < filesList; i++) {
Console.WriteLine ("\tFile : " + Path.GetFileName (showFiles [i]));
}
// When listing is finished, ask the user to select the file he want to open
Console.WriteLine (@"Type the filename (With extension) you want to open:");
String userChoice = folderToOpen + Console.ReadLine ();
Process.Start (userChoice); // Loading with default application regarding the file extension
}
publicstaticvoidopenfile()
{
//列出FormattedDocuments目录中的文件
字符串[]showFiles=Directory.GetFiles(“格式化文档”);
int filesList=showFiles.GetUpperBound(0)+1;
常量字符串folderToOpen=@“FormattedDocuments/”;
Console.WriteLine(“这是文件列表:”);
对于(int i=0;i
我的问题是:
您好,我已将此代码改编为我的代码,但是否可以在每行前面添加一个数字,然后键入此数字以打开指定的文件,而不是键入完整的文件名?谢谢。当然你可以在foreach循环上面加一个int,然后在debug.writeline(yourvariablename++)之后,在debug.writeline(yourvariablename+f)中更改debug.writeline。我尝试了多个代码,但我无法使用列表代码并在列出的每个文件前面返回一个数字,另外,使用此号码调用与文件关联的process.start。
DirectoryInfo directory = new DirectoryInfo(@"C:\temp");
FileInfo[] files = directory.GetFiles();
var filtered = files.Select(f => f)
.Where(f => (f.Attributes & FileAttributes.Hidden) == 0);
foreach (var f in filtered) {
Debug.WriteLine(f);
}