Introduction
It was just another day when there was a need to include an ini file with my package. This file is to be modified by the client. The file
cannot be written to the installation directory where the package executable resides due to permission access
issues on Windows 7.
Using the code
Workaround is as follows:
- Within your Visual Studio development area, Solution Explorer (on right hand side of your project) right click on your project and choose
"Properties" option. Now choose "Resources" tab.
- Simply select "Add Resources" and navigate to your file. I have called my file DataFile.txt.
That’s it, we are done with resources. Now we need to be able to read and save this file from resources to a directory which all users have access to.
We need to implement the following to achieve this.
Import these:
Imports System.Resources
Imports System.Text
Imports System.IO
Now the function to save our file from resources to the directory for all users to access:
Private Function SaveFromResourcess(ByRef errorMsg As String)As Boolean
dim iniBytes() As Byte
dim iniFileLocation As String = _
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & _
"\DataFile.ini "
Dim obj As Object = My.Resources.ResourceManager.GetObject("DataFile", _
System.Globalization.CultureInfo.CurrentUICulture)
Try
iniBytes = Encoding.ASCII.GetBytes(obj.ToString)
If (File.Exists(iniFileLocation)= True) Then
File.Delete(iniFileLocation)
End If
File.WriteAllBytes(iniFileLocation, iniBytes)
Catch ex As Exception
errorMsg="File " & iniFileLocation & " not saved." & _
"Caused is " & ex.Message & "."
Return False
End Try
Return true
End Sub