我有遵循下面的模式文件路徑:如何獲取此文件路徑的最後部分?
一些\文件\路徑\基地\ YYYY \ MM \ DD \ HH \毫米\ Random8.3
我想從2012年及以後的一切提取,但問題是,雖然右側是標準的,但每個記錄的基本目錄可能不同。
下面是兩個例子:
C:\ TEMP \ X \ 2012 \ 08 \ 27 \ 18 \ 35 \ wy32dm1q.qyt
返回:2012\08\27\18\35\wy32dm1q.qyt
d:\溫度\ X \ Y \ 2012 \ 08 \ 27 \ 18 \ 36 \ tx84uwvr.puq
返回:2012\08\27\18\36\tx84uwvr.puq
眼下我在2012年抓取了LastIndexOf(Path.DirectorySeparatorChar)
N次以獲取字符串的索引,然後從該索引獲取子字符串。但是,我有一種感覺,也許有更好的辦法?
我撤回我的答案,我只是注意到在你的問題中「超越」。我會寫一個匹配正則表達式「\\\ d {4} \\」的函數,將以下10個字符轉換爲DateTime,然後與01-01-2012進行比較。不幸的是,我缺乏正則表達式的專業知識,無法提供權威的答案。 –
你有這個基目錄作爲你的應用程序中的變量嗎? –
你也可以使用正則表達式,但爲什麼?將LastIndexOf打在for循環中,也許將它包裝在自己的方法中是很好的。 –