Home page  

Products

CipherLite.NET

CipherSafe.NET

Downloads

Resources

Articles

Libraries

Samples

Site Map

BIGGER FONTsmaller font

How To: Use CipherSafe™ API to Get Application Profile Values (C#/VB.NET)

The code below demonstrates how to use the CipherSafe™ API to retrieve application profile values defined for the caller application. Code samples are provided in C# and Visual Basic.NET.


C# code

[printer-friendly version]
///////////////////////////////////////////////////////////////////////////////
// SAMPLE: Using CipherSafe to retrieve application profile values.
//
// This sample requires CipherSafe.NET to be installed on the system.
// To run the sample, create a new Visual C# project using the Console
// Application template and replace the contents of the Class1.cs file with
// the code below. Add a reference to CipherSafe.dll, which should be
// installed by default to C:\Program Files\Obviex\CipherSafe folder (the
// installation folder may include product version number).
//
// When you first run the application, it may generate an error,
// because the application profile may not have been defined. To fix it,
// open the CipherSafe.NET GUI tool and create an application profile
// for the application executable (make sure you point to the correct
// executable file located in the bin\Debug or bin\Release directory).
// Give the application any name you want and add at least one profile
// value with the name 'Password' (without quotes). Run the application.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// 
// Copyright (C) 2002 Obviex(TM). All rights reserved.
// 
using System;
using Obviex.CipherSafe;

/// <summary>
/// Shows how to retrieve application profile values using the CipherSafe API.
/// </summary>
public class CipherSafeDemo
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        // Declare variable which will hold current application profile.
        ApplicationProfile  app = null;
        
        try
        {
            // Display the path to the application, which profile
            // we are about to retrieve. GetCurrentPath can be helpful
            // if we fail to get application profile, because it can
            // show us what CipherSafe thinks the current application is.
            Console.WriteLine("Processing " + 
                    ApplicationProfile.GetCurrentPath(Category.ExeFile) +
                    "...");

            // Get profile of the running executable file.
            app = ApplicationProfile.GetProfile(Category.ExeFile);
            
            // Option 1: Get profile value by name.
            Console.WriteLine("Retrieving profile value by name...");
            Console.WriteLine("Password = " + app["Password"]);
            
            // Option 2: Iterate through all profile values.
            Console.WriteLine("Retrieving all profile values...");
            foreach (ProfileItem item in app)
            {
                // Do not use ProfileItem.Value, because it holds
                // encrypted value.
                Console.WriteLine(String.Format("{0} = {1}",
                                   item.Name, 
                                   app[item.Name]));
            }
        }
        catch (DetailedException ex)
        {
            // Show all error messages.
            Console.WriteLine(ex.Messages);
        }
        catch (Exception ex)
        {
            // Show immediate error message.
            Console.WriteLine(ex.Message);
        }
    }
}
//
// END OF FILE
///////////////////////////////////////////////////////////////////////////////

^ Back to top  


VB.NET code

[printer-friendly version]
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' SAMPLE: Using CipherSafe to retrieve application profile values.
'
' This sample requires CipherSafe.NET to be installed on the system.
' To run the sample, create a new Visual Basic.NET project using the Console
' Application template and replace the contents of the Module1.vb file with
' the code below. Add a reference to CipherSafe.dll, which should be
' installed by default to C:\Program Files\Obviex\CipherSafe folder (the
' installation folder may include product version number).
'
' When you first run the application, it may generate an error,
' because the application profile may not have been defined. To fix it,
' open the CipherSafe.NET GUI tool and create an application profile
' for the application executable (make sure you point to the correct
' executable file located in the bin\Debug or bin\Release directory).
' Give the application any name you want and add at least one profile
' value with the name 'Password' (without quotes). Run the application.
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' 
' Copyright (C) 2002 Obviex(TM). All rights reserved.
'
Imports System
Imports Obviex.CipherSafe

Module Module1

    ' <summary>
    ' The main entry point for the application.
    ' </summary>
    Sub Main()

        ' Declare variable which will hold current application profile.
        Dim app As ApplicationProfile = Nothing
        
        Try
            ' Display the path to the application, which profile
            ' we are about to retrieve. GetCurrentPath can be helpful
            ' if we fail to get application profile, because it can
            ' show us what CipherSafe thinks the current application is.
            Console.WriteLine("Processing " & _ 
                    ApplicationProfile.GetCurrentPath(Category.ExeFile) & _
                    "...")

            ' Get profile of the running executable file.
            app = ApplicationProfile.GetProfile(Category.ExeFile)
            
            ' Option 1: Get profile value by name.
            Console.WriteLine("Retrieving profile value by name...")
            Console.WriteLine("Password = " & app("Password"))
            
            ' Option 2: Iterate through all profile values.
            Console.WriteLine("Retrieving all profile values...")
            Dim item As ProfileItem
            For Each item In app
                ' Do not use ProfileItem.Value, because it holds
                ' encrypted value.
                Console.WriteLine(String.Format("{0} = {1}" & _
                                   item.Name & _ 
                                   app(item.Name)))
            Next
        Catch ex As DetailedException
        
            ' Show all error messages.
            Console.WriteLine(ex.Messages)
        
        Catch ex As Exception
        
            ' Show immediate error message.
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Module
'
' END OF FILE
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

^ Back to top

Copyright © 2002-2010 Obviex™ | All rights reserved | Legal | Privacy | Contact us