LoginSignup
0
0

More than 5 years have passed since last update.

[メモ]フルパスからファイル名までディレクトリの色々

Last updated at Posted at 2017-11-14

状況

ファイル選択時に、選択以外の操作(作成、削除、変更等)を制御したかったため、
フォルダダイアログでフォルダ選択->フォルダ内の指定拡張子ファイル取得->ファイル選択
という手順を踏んでいたら、ディレクトリ操作系をまとめといたら便利だと思った。

困ったこと

特になし。

解決?

以下、色んなディレクトリ操作
・フルパスからフォルダ名、ファイル名を取得

Dim fullpath As String = "C:\test\test.txt"
Dim filedir As String = System.IO.Path.GetDirectoryName(fullpath)
Dim filename As String = System.IO.Path.GetFileName(fullpath)

・System.IO.Path名前空間からチョコをもらえなかった人用

Dim fullpath As String = "C:\test\test.txt"
Dim find_lastslash As String = fullpath.LastIndexOf("\") + 1
Dim filedir As String = fullpath.Substring(0, find_lastslash)
Dim filename As String = fullpath.Substring(find_lastslash, fullpath.Length - find_lastslash)

・フォルダ内の全ファイル名取得(フルパス)

Dim filedir As String = "C:\test\"
'Or Dim filedir As String = "C:\test" 
Dim allfile() As String = System.IO.Directory.GetFiles(filedir)

・フォルダ内の指定拡張子ファイル取得(フルパス)(ex. csv拡張子)

Dim filedir As String = "C:\test\"
'Or Dim filedir As String = "C:\test" 
Dim csvfile() As String = System.IO.Directory.GetFiles(filedir, "*.csv")

・指定フォルダ以下の全ファイル取得(フルパス)

Dim filedir As String = "C:\test\"
'Or Dim filedir As String = "C:\test"
Dim allfile() As String = System.IO.Directory.GetFiles(filedir,"*",System.IO.SearchOption.AllDirectories)

使用方法

コピペ。
世の中便利なコードはたくさん溢れていることを考えると、
僕のような微妙な立ち位置のプログラマなんていらなくなっちゃうね。
2017.11.15追記
ゴミ箱は扱いが特殊で、Cドライブ直下を参照してC:$Recycle.Binを読み込んじゃうとエラー吐いちゃうと、コメントいただきました。
まだ解決策見つけてないので、見つけたら修正しまうす。

コメントもらえるなんて僕は幸せもんだぁ。

VB.NET用の便利コードは下記。
https://github.com/keuxkeu/useful-codes-for-vb.net
まとめたものを追加していくので、気が向いたらどぞ。
(大体未完成品)

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0