VB.NET Tips - ファイル上書きコピー
指定されたコピー元ファイルの存在を確認し、ファイルの FileInfo を取得し、
FileInfoのファイル長が0より大きい場合に、指定されたコピー先ファイルに上書きコピーを行う関数です。
コピー処理は、System.IO.File.Copy により上書きフラグをONして処理を行います。
System.IO.FileInfo クラスについて
■コンストラクタ
Public Sub New ( fileName )
fileName:
ファイルのドライブ名からの完全パス名または相対ファイル名。
■プロパティ抜粋
・Length (Long) :現在のファイルのサイズをバイト単位で取得します。
・Name (String) :ファイルの名前を取得します。ファイル拡張子が含まれています。
・FullName (String) :ファイルのフルパス名を取得します。
・DirectoryName(String) :ディレクトリの絶対パスを表す文字列。
・Extension (String) :ファイルの拡張子部分を表す文字列を取得します。
ファイル上書きコピー
01 | ''' ----------------------------------------------------------------------------- |
02 | ''' <summary> |
03 | ''' ファイル上書きコピー |
04 | ''' </summary> |
05 | ''' <param name="astrSrcFilePath">コピー元ファイルフルパス名 |
06 | ''' <param name="astrDesFilePath">コピー先ファイルフルパス名 |
07 | ''' <returns>True:正常終了, False:エラー</returns> |
08 | ''' <remarks>コピー元ファイルが存在しない場合、処理無し</remarks> |
09 | ''' ----------------------------------------------------------------------------- |
10 | Public Function CopyFileOverwrite( ByVal astrSrcFilePath As String , |
11 | ByVal astrDesFilePath As String ) As Boolean |
12 | '戻り値初期化 |
13 | CopyFileOverwrite = False |
14 | Try |
15 | 'コピー元ファイル存在確認 |
16 | If System.IO.File.Exists(astrSrcFilePath) = False Then |
17 | '入力用ファイルが存在しない場合は、OKとする |
18 | Return True |
19 | End If |
20 |
21 | Dim fileinfo As New System.IO.FileInfo(astrSrcFilePath) |
22 | 'ファイルサイズ取得 |
23 | If fileinfo.Length = 0 Then |
24 | 'サイズ0の場合は、処理無しで正常戻り |
25 | Return True |
26 | End If |
27 |
28 | 'コピー元入力用ファイルからコピー先ファイル上書きコピー |
29 | System.IO.File.Copy(astrSrcFilePath, astrDesFilePath, True ) |
30 | '正常終了 |
31 | Return True |
32 |
33 | Catch ex As Exception |
34 | 'エラー処理が必要な場合は、ここに記述する |
35 | End Try |
36 | End Function |