Tuesday, September 05, 2006 4:04 AM
keremkusmezer
Little Wrapper To Control Ngen From Your Code
I wrote a little wrapper for Ngen .Net 2.0. It misses some features as ques , which will be implemented very soon.
Namespace
CSFUpdater
Public NotInheritable Class NgenHelper
#
Region "Private Variables"
Private Const NGENPATH As String = "ngen.exe"
Private Shared NgenFullPath As String
Private Shared ngenFound As Boolean
Private Shared resultCache As System.Text.StringBuilder
#
End Region
Shared Sub New()
NgenFullPath =
String.Format("{0}{1}", System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory, NGENPATH)
ngenFound = (System.IO.File.Exists(NgenFullPath))
End Sub
Private Sub New()
End Sub
Public Shared Function RegisterGivenDlls(ByVal ParamArray fileName As String()) As String
Return ""
End Function
Public Shared Function UnRegisterGivenDlls(ByVal ParamArray fileName As String()) As String
Return ""
End Function
Public Shared Function RegisterWholeApplicationDlls(Optional ByVal Debugable As Boolean = False) As String
Dim files() As String = System.IO.Directory.GetFiles(CSF.CO.ReflectionDynamicCompile.ReflectionUtilities.GetExecutingAssemblyPath, "*.dll")
Dim registerCache As New System.Text.StringBuilder()
Try
For Each file As String In files
registerCache.Append(StartGivenProcess(
String.Format(" install {0}{1}{2}", Chr(34), file, Chr(34)), CSF.CO.ReflectionDynamicCompile.ReflectionUtilities.GetExecutingAssemblyPath))
Next
Catch
End Try
Return registerCache.ToString()
End Function
Public Shared Function UnRegisterWholeApplicationDlls(Optional ByVal Debugable As Boolean = False) As String
Dim files() As String = System.IO.Directory.GetFiles(CSF.CO.ReflectionDynamicCompile.ReflectionUtilities.GetExecutingAssemblyPath, "*.dll")
Dim registerCache As New System.Text.StringBuilder()
Try
For Each file As String In files
registerCache.Append(StartGivenProcess(
String.Format(" uninstall {0}{1}{2}", Chr(34), file, Chr(34)), CSF.CO.ReflectionDynamicCompile.ReflectionUtilities.GetExecutingAssemblyPath))
Next
Catch
End Try
Return registerCache.ToString()
End Function
Public Shared Function RefreshNgenCache() As String
Return StartGivenProcess(" update", "")
End Function
Private Shared Function StartGivenProcess(ByVal commandLine As String, ByVal workingDirectory As String) As String
Dim startInfo As ProcessStartInfo
resultCache =
New System.Text.StringBuilder
Dim runningProcess As New Process
startInfo = runningProcess.StartInfo
With startInfo
.WindowStyle = ProcessWindowStyle.Hidden
.RedirectStandardOutput =
True
.RedirectStandardError =
True
.UseShellExecute =
False
If Not (String.IsNullOrEmpty(workingDirectory)) Then
.WorkingDirectory = workingDirectory
End If
End With
AddHandler runningProcess.OutputDataReceived, AddressOf OutputDataReceived
runningProcess.Start()
With runningProcess
.BeginOutputReadLine()
.BeginErrorReadLine()
.WaitForExit()
End With
Return resultCache.ToString()
End Function
Private Shared Sub OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
resultCache.Append(e.Data)
End Sub
Private Shared Sub NgenLibraries()
Try
Dim startInfo As ProcessStartInfo
Dim ngenProcess As Process
Dim files() As String = System.IO.Directory.GetFiles(CSF.CO.ReflectionDynamicCompile.ReflectionUtilities.GetExecutingAssemblyPath, "*.dll")
Try
For Each file As String In files
startInfo =
New ProcessStartInfo(String.Format("{0}{1}", System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory, NGENPATH), String.Format(" install {0}{1}{2}", Chr(34), file, Chr(34)))
startInfo.WorkingDirectory = CSF.CO.ReflectionDynamicCompile.ReflectionUtilities.GetExecutingAssemblyPath
ngenProcess = Process.Start(startInfo)
ngenProcess.WaitForExit()
Next
Catch
End Try
Catch ex As System.Exception
End Try
End Sub
End Class
End
Namespace