C#如何从目录路径grep最后一个单词? [英] C# How to grep the last word from a directory path?

查看:183
本文介绍了C#如何从目录路径grep最后一个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,从日志文本文件greps出各种目录路径,并根据该字打印各种结果。

I have a program that "greps" out various directory paths from a log text file and prints various results according to the word.

目录路径示例:


  • C:/ Documents and设置/所有用户/桌面/ AccessData FTK Imager.lnk

  • C:/Documents and Settings/All Users/Desktop/AccessData FTK Imager.lnk

C:/文档和设置/所有用户/开始菜单/程序/ AccessData

C:/Documents and Settings/All Users/Start Menu/Programs/AccessData

C:/ Documents and Settings / Administrator / Desktop / AccessData FTK Imager.exe:Zone.Identifier

C:/Documents and Settings/Administrator/Desktop/AccessData FTK Imager.exe:Zone.Identifier

因此,如何在最后一个/之后删除文件或文件夹名称?这是为了帮助程序识别文件和文件夹。请注意多个。并在目录路径中找到空白处。 Imager.exe:Zone.Identifier。因此,很难使用 if(!name.contains()。)

Therefore how can I grep out the file or folder name after the last "/"? This is to help the program to identify between files and folder. Please do take note of the multiple "." and white spaces found within a directory paths. etc "Imager.exe:Zone.Identifier". Therefore it is difficult to use if(!name.contains()".")

等。如何从路径STRING获取AccessData FTK Imager.lnk或AccessData或AccessData FTK Imager.exe:Zone.Identifier!

Etc. How to get the "AccessData FTK Imager.lnk" or "AccessData" or "AccessData FTK Imager.exe:Zone.Identifier" from the path STRING?!

可能有人请告知方法或代码来解决这个问题?谢谢!

May someone please advise on the methods or codes to solve this problem? Thanks!

代码:

if (!token[7].Contains("."))
                {    

                    Console.WriteLine("The path is a folder?");

                    Console.WriteLine(token[7]);

                    Console.WriteLine(actions);

                    MacActions(actions);

                    x = 1;
                } 


推荐答案

在工作中使用Path类使用文件路径,并在使用实际文件和文件夹时使用文件和目录类。

Use the Path class when working with file paths, and use the File and Directory class when working with actual files and folders.

string str1=@"C:/Documents and Settings/All Users/Desktop/AccessData FTK Imager.lnk";
string str2=@"C:/Documents and Settings/All Users/Start Menu/Programs/AccessData";
string str3=@"C:/Documents and Settings/Administrator/Desktop/AccessData FTK Imager.exe:Zone.Identifier";

Console.WriteLine(Path.GetFileName(str1));
Console.WriteLine(Path.GetFileName(str2));
Console.WriteLine(Path.GetFileName(str3));

输出:

AccessData FTK Imager.lnk
AccessData
Zone.Identifier <-- it chokes here because of the :

此类对字符串进行操作,因为我的系统上没有特定的文件和/或文件夹。另外,不可能确定 AccessData 是否是一个文件夹或没有扩展名的文件。

This class operates on strings, as I do not have those particular files and/or folders on my system. Also it's impossible to determine whether AccessData is meant to be a folder or a file without an extension.

我可以使用一些常识,并声明具有扩展名的所有内容作为文件( Path.GetFileExtension 可以在这里使用)和其他一切都是一个文件夹。

或者我可以检查它有关的字符串确实是我的机器上的文件或文件夹使用( File.Exists Directory.Exists

I could use some common sense and declare everything with an extension to be a file (Path.GetFileExtension can be used here) and everything else to be a folder.
Or I could just check it the string in question is indeed a file or a folder on my machine using (File.Exists and Directory.Exists respectively).

if (File.Exists(str2))
    Console.WriteLine("It's a file");
else if (Directory.Exists(str2))
    Console.WriteLine("It's a folder");
else
    Console.WriteLine("It's not a real file or folder");

这篇关于C#如何从目录路径grep最后一个单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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