Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Visual Studio Resources usage

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 May 2012CPOL 6.4K   2  
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:

VB
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:

VB
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --