[VB.NET,file di risorse esterno], mi date una mano, p.f. ?

martedì 28 febbraio 2012 - 08.12
Tag Elenco Tags  VB.NET

Zan Profilo | Newbie

Premetto che ieri ho postato in "App. Visual Studio 6.0" e, non ricevendo nessun contatto ho pensato di acer sbagliato stanza, ripropongo così il mio problema in questa.

Salve a tutti,
Premetto che prima ho fatto diverse prove ricerche su Google...e, non essendo proprio un genio informatico mi sino arreso ed eccomi qui...
Stavo cercando di fare un codice che mi permettesse di archiviare dei files in una risorsa esterna e,girovangando in rete ho trovato questo esempio che permette la creazione di un file di risorse attraverso VB.
Funziona tutto perfettamente ma avrei bisogno di inserire, oltre alle immagini come da esempio, anche altri tipi di file, .ZIP, .PDF, .DOC etc.
Come potrei fare ?
Il codice dichiara un oggetto immagine, io che tipo dovrei dichiarare per i file menzionati prima ?


'===================================
' Code to Create a Resource.
Dim objImage As Image <=================== ?
Dim strString As String
Dim rsw As ResourceWriter

' objImage is the Image that will be added as a resource.
objImage = Image.FromFile("d:\\lock.png")

' strString is the string that will be added as a resource.
strString = "This is the string from resource"

'Creates a resource writer instance to write to MyResource.resources.
rsw = New ResourceWriter("MyResource.resources")

'Adds the image to the resource.
' "MyImage" is the name that the Image is identified as in the resource.
rsw.AddResource("MyImage", objImage)

'Adds the string to the resource.
' "MyText" is the name that the string is identified as in the resource.
rsw.AddResource("MyText", strString)

rsw.Close()
MessageBox.Show("Resource Is Created.....")
'===================================


Grazie mille in anticipo a tutti quelli che vorranno/potranno aiutarmi
Saluti
R.Zanni

AntCiar Profilo | Expert

Ciao.

Adattando il codice che hai postato viene fuori questo:


Private Sub AddFileToResource(ByVal pathFile As String) '=================================== ' Code to Create a Resource. Dim objData As Byte() = Nothing Dim strString As String Dim rsw As Resources.ResourceWriter ' estraggo il contenuto del file in un array di Byte objData = IO.File.ReadAllBytes(pathFile) strString = IO.Path.GetFileNameWithoutExtension(pathFile) 'Creates a resource writer instance to write to MyResource.resources. rsw = New Resources.ResourceWriter("MyResource.resources") 'Adds the image to the resource. ' "MyImage" is the name that the Image is identified as in the resource. rsw.AddResource(strString, objData) rsw.Close() MessageBox.Show("Resource Is Created.....") '=================================== End Sub

ciao ciao
Cristian Barca

Zan Profilo | Newbie

grazie mille, funziona benissimo.
Se puoi/vuoi, come potrei fare per il processo inverso ?
Risalvare il file inserito nuovamente su disco ?

Grazie nuovamente per il tuo prezioso aiuto

AntCiar Profilo | Expert

Ciao.

Per estrarre una risorsa basta usare questo codice:

Il codice sorgente non è stato renderizzato qui
perchè non c'è sufficiente spazio.
Clicca qui per visualizzarlo in una nuova finestra


l'unica cosa da modificare nel codice per inglobare le risorse è questo: cambiare la riga

IO.Path.GetFileNameWithoutExtension(pathFile)

con

IO.Path.GetFileName(pathFile)

questo perchè la prima riga salva la risorsa con il nome del file senza estensione e quindi dopo se la devi risalvare in un file perdi l'informazione originale. Con la seconda praticamente dai alla risorsa il nome originale del file e quindi puoi risalvarlo senza problemi.
Cristian Barca

Zan Profilo | Newbie

ciao,
ho un errore di questo tipo nel recupero...

'=======
Private Sub btnCrea_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrea.Click
'===================================
' Code to Create a Resource.
Dim objData As Byte() = Nothing
Dim strString As String
Dim rsw As Resources.ResourceWriter

' estraggo il contenuto del file in un array di Byte
objData = IO.File.ReadAllBytes("D:\RESOURCE.ZIP")
strString = IO.Path.GetFileName("D:\RESOURCE.ZIP")

'Creates a resource writer instance to write to MyResource.resources.
rsw = New Resources.ResourceWriter("MyResourceZIP.resources")

'Adds the file to the resource.
rsw.AddResource(strString, objData)
rsw.Close()
MessageBox.Show("Resource Is Created.....")
'===================================

End Sub

Private Sub btnRecupera_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecupera.Click
'Try
Dim myAssembly As System.Reflection.Assembly
myAssembly = Me.GetType.Assembly

Dim myManager As New System.Resources.ResourceManager("MyResourceZIP.resources", myAssembly)
Dim resContent As Byte() = CType(myManager.GetObject("RESOURCE.ZIP"), Byte())
IO.File.WriteAllBytes("c:\pippo.zip", resContent)


'Catch ex As Exception
'End Try

End Sub


ciao e grazie

AntCiar Profilo | Expert

Ciao.
Io uso quel codice però con risorse incorporate in progettazione. Forse incorporate dinamicamente non riesce a vederle.
Prova a cercare sulla rete qualcosa di meglio.

Ciao
Cristian Barca

Zan Profilo | Newbie

grazie mille per la tua collaborazione, proverò ancora a cercare.
Ti auguro un'ottima giornata

Ciao
R.Zanni

Zan Profilo | Newbie

Ciao, ci sono riuscito !!!!!

Ti posto l codice, magari potrebbe servire.......
Private Sub btnRecupera_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecupera.Click
Try

Dim rm As ResourceManager
Dim resContent As Byte()
rm = ResourceManager.CreateFileBasedResourceManager("MyResourceZIP", ".", Nothing)
resContent = rm.GetObject("RESOURCE.ZIP")
IO.File.WriteAllBytes("d:\pippo.zip", resContent)

Catch ex As Exception
End Try

End Sub

ciao e buona giornata
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5