.net 读取资源文件内容示例代码

2013年6月23日

Sub Main()
Dim res As New ResourceReader(“CustomControls.Properties.Resources.resources”)
‘该文件放到bin
Dim dics As IDictionaryEnumerator = res.GetEnumerator()
While dics.MoveNext()

 

Dim filepath As String = dics.Key.ToString()
filepath = Path.Combine(“d:\aa\”, filepath)
‘保存到指定目录
filepath = Path.GetFullPath(filepath)
Dim p = Path.GetDirectoryName(filepath)
‘要创建的目录
If Not Directory.Exists(p) Then
Directory.CreateDirectory(p)
End If

If TypeOf dics.Value Is System.Drawing.Bitmap Then
Dim ms As New IO.MemoryStream
CType(dics.Value, System.Drawing.Bitmap).Save(ms, CType(dics.Value, System.Drawing.Bitmap).RawFormat)
My.Computer.FileSystem.WriteAllBytes(filepath & “.png”, ms.ToArray, False)
Continue While
End If

If TypeOf dics.Value Is String Then
Dim s As String = DirectCast(dics.Value, String)
My.Computer.FileSystem.WriteAllText(filepath & “.xml”, s, False)
End If

If TypeOf dics.Value Is Byte() Then

Dim s As Byte() = DirectCast(dics.Value, Byte())
Dim fileSize As Integer = CInt(s.Length)
‘Dim fileContent As Byte() = New Byte(fileSize – 1) {}
‘s.Read(fileContent, 0, fileSize)
Dim fs As FileStream
Dim fi As FileInfo = New System.IO.FileInfo(filepath)
fs = fi.OpenWrite()
fs.Write(s, 0, fileSize)
fs.Close()
End If

 

 

 
End While

res.Close()
End Sub

声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: .net 读取资源文件内容示例代码
本文的评论功能被关闭了.