using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Win32;
namespace ChgOdpRefVersion
{
static class Program
{
static void Main(string[] args)
{
if (args.Length < 2 || !args[0].Contains(".dll") ||
!Regex.IsMatch(args[1], @"\d+\.\d+\.\d+\.\d+"))
{
Console.WriteLine("Syntax Example: ChgOdpRefVersion Boo.dll 2.112.3.0");
return;
}
string dllFile = args[0].Trim('\"'), version = args[1];
string outDllFile =
Path.Combine(Path.GetDirectoryName(dllFile),
Path.GetFileNameWithoutExtension(dllFile) + ".odp-" + version + ".dll");
string ilFile = Path.GetTempFileName() + ".il";
ildasm(dllFile, ilFile);
StringBuilder sb = new StringBuilder();
bool found = false;
using (StreamReader sr = new StreamReader(ilFile))
{
string line = null;
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith(".assembly extern Oracle.DataAccess"))
{
sb.AppendFormat(@"
.assembly extern Oracle.DataAccess
{{
.publickeytoken = (89 B4 83 F4 29 C4 73 42 )
.ver {0}
}}
", version.Replace(".", ":"));
do
{
line = sr.ReadLine();
} while (!line.StartsWith("}"));
sb.Append(sr.ReadToEnd());
found = true;
break;
}
else
{
sb.AppendLine(line);
}
}
}
if (found)
{
File.WriteAllText(ilFile, sb.ToString());
ilasm(ilFile, outDllFile);
Console.WriteLine("Done!");
}
else
Console.WriteLine("Oracle.DataAccess reference not found!");
}
public static void ildasm(string dllPath, string ilPath)
{
exec(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
@"Microsoft SDKs\Windows\v6.0A\bin\ildasm.exe"),
string.Format("\"{0}\" /output:\"{1}\"", dllPath, ilPath));
}
public static void ilasm(string ilPath, string dllPath)
{
exec(Path.Combine(GetFrameworkDirectory(), "ilasm.exe"),
string.Format("\"{0}\" /dll /output:\"{1}\"", ilPath, dllPath));
}
public static void exec(string program, string arguments)
{
ProcessStartInfo psi = new ProcessStartInfo(program, arguments);
Process p = Process.Start(psi);
p.WaitForExit();
}
//REF: http://bit.ly/PLA9Zw
public static string GetFrameworkDirectory()
{
string framworkRegPath = @"Software\Microsoft\.NetFramework";
RegistryKey netFramework =
Registry.LocalMachine.OpenSubKey(framworkRegPath, false);
string installRoot =
netFramework.GetValue("InstallRoot").ToString();
string version = string.Format(@"v{0}.{1}.{2}\",
Environment.Version.Major, Environment.Version.Minor, Environment.Version.Build);
return System.IO.Path.Combine(installRoot, version);
}
}
}