Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Visual Studio Resources usage

0.00/5 (No votes)
7 May 2012 1  
How to use Resources in Visual Studio.

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:

  1. 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.
  2. 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here