hi,

This is a small class I made that allows you to expand the variables in a string. Much like what the windows api call does, this also allows you to define your own variable name and data. This is still in testing mode so there maybe a few things I need to fix, if you have any suggestions please feel free to let me know.

Hope you find this code us full.

Class code. VarStrExpand.cs

csharp Code:
  1. using System.Collections.Generic;
  2.  
  3. //Expand vraible class by Ben Jones, Ver Alpha 1.0
  4. //Allows you to define your own variable names and data.
  5. //Last-updated 19/01/15
  6.  
  7. namespace ExpEnv
  8. {
  9.     class VarStrExpand
  10.     {
  11.         private struct TVARIABLES
  12.         {
  13.             public string sVarName;
  14.             public string sVarData;
  15.         };
  16.  
  17.         private string m_src = null;
  18.         private string m_ret = null;
  19.  
  20.         private List<TVARIABLES> _vars;
  21.  
  22.         public VarStrExpand()
  23.         {
  24.             _vars = new List<TVARIABLES>();
  25.         }
  26.  
  27.         public void VariableAdd(string sName, string sData)
  28.         {
  29.             //Add variable and data.
  30.             TVARIABLES T;
  31.             T.sVarName = sName.ToUpper();
  32.             T.sVarData = sData;
  33.             _vars.Add(T);
  34.         }
  35.  
  36.         public string Source
  37.         {
  38.             get
  39.             {
  40.                 return m_src;
  41.             }
  42.             set
  43.             {
  44.                 m_src = value;
  45.             }
  46.         }
  47.  
  48.         public override string ToString()
  49.         {
  50.             //Expland the variables in the source string.
  51.             ExpandVar(this.Source);
  52.             //Return result.
  53.             return m_ret;
  54.         }
  55.  
  56.         public void Clear()
  57.         {
  58.             //This just free eveything up.
  59.             this.m_ret = null;
  60.             this.m_src = null;
  61.             this._vars.Clear();
  62.         }
  63.  
  64.         private string _VarGet(string sCheckName)
  65.         {
  66.             int J = 0;
  67.             string sRet = "";
  68.  
  69.             while (J < _vars.Count)
  70.             {
  71.                 if (_vars[J].sVarName == sCheckName)
  72.                 {
  73.                     //Get variable data.
  74.                     sRet = _vars[J].sVarData;
  75.                 }
  76.                 //INC counter.
  77.                 J++;
  78.             }
  79.             return sRet;
  80.         }
  81.  
  82.         private void ExpandVar(string source)
  83.         {
  84.             int I = 0;
  85.             string sVriable = "";
  86.             string sData = "";
  87.  
  88.             bool in_var = false;
  89.  
  90.             //Append null char to end., just makes sure out I index
  91.             //does not go out of range sure there is a better way
  92.             //but it seems to work for my purpose.
  93.  
  94.             source += '\0';
  95.  
  96.             for (I = 0; I < source.Length; I++)
  97.             {
  98.                 if (source[I] == '%')
  99.                 {
  100.                     //Set wether in or out of variable string %var%
  101.                     in_var = (!in_var);
  102.                     //Move index along to skip %
  103.                     I += 1;
  104.                 }
  105.                 if (in_var)
  106.                 {
  107.                     //Get variable name.
  108.                     sVriable += source[I].ToString().ToUpper();
  109.                 }
  110.                 else
  111.                 {
  112.                     if (sVriable.Length != 0)
  113.                     {
  114.                         //Get variable data.
  115.                         sData = _VarGet(sVriable);
  116.                         //Check variable length.
  117.                         if (sData.Length == 0)
  118.                         {
  119.                             //Reapend variable tags %var%
  120.                             sData = "%" + sVriable + "%";
  121.                         }
  122.                     }
  123.  
  124.                     //if (I >= source.Length)
  125.                     //{
  126.                     //    break;
  127.                     //}
  128.  
  129.                     //Build string
  130.                     m_ret += sData + source[I];
  131.                     //Clear variable.
  132.                     sVriable = "";
  133.                     sData = "";
  134.                 }
  135.             }
  136.             //return sBuffer;
  137.         }
  138.     }
  139. }

Example

csharp Code:
  1. //Example
  2.             VarStrExpand v = new VarStrExpand();
  3.  
  4.             //Source to expland.
  5.             v.Source = "%drv%\\%Docs%\\%Folder1%\\%File%";
  6.  
  7.             //Add custom variables and data.
  8.             v.VariableAdd("drv", "D:");
  9.             v.VariableAdd("docs", "Work\\microsoft");
  10.             v.VariableAdd("folder1", "docs");
  11.             v.VariableAdd("file", "copyright.txt");
  12.             //Show result.
  13.             MessageBox.Show(v.ToString());
  14.             //Clear up the class.
  15.             v.Clear();