ShittyAdvice

CodeDOM

Jun 15th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.42 KB | None | 0 0
  1. Imports System.CodeDom.Compiler
  2.  
  3. Public Class CodeDOM
  4.     Private _Location As String
  5.     Public Property Location() As String
  6.         Get
  7.             Return _Location
  8.         End Get
  9.         Set(ByVal value As String)
  10.             _Location = value
  11.         End Set
  12.     End Property
  13.  
  14.     Private _Version As Dictionary(Of String, String)
  15.     Public Property Version() As Dictionary(Of String, String)
  16.         Get
  17.             Return _Version
  18.         End Get
  19.         Set(ByVal value As Dictionary(Of String, String))
  20.             _Version = value
  21.         End Set
  22.     End Property
  23.  
  24.     Private _ICO As String
  25.     Public Property ICO() As String
  26.         Get
  27.             Return _ICO
  28.         End Get
  29.         Set(ByVal value As String)
  30.             _ICO = value
  31.         End Set
  32.     End Property
  33.  
  34.     Private _References() As String
  35.     Public Property References() As String()
  36.         Get
  37.             Return _References
  38.         End Get
  39.         Set(ByVal value As String())
  40.             _References = value
  41.         End Set
  42.     End Property
  43.     '@param {ParamArray} ReferencesIn: References to be compiled
  44.     Public Sub AddReference(ParamArray ReferencesIn() As String)
  45.         For Each Reference In ReferencesIn
  46.             Array.Resize(References, References.Length + 1)
  47.             References(References.Length - 1) = Reference
  48.         Next
  49.     End Sub
  50.  
  51.     'Settings all the default values
  52.     Sub New()
  53.         ICO = IO.Path.GetTempPath & "\iCompiler.ico"
  54.         References = New String() {}
  55.         Version = New Dictionary(Of String, String)
  56.     End Sub
  57.  
  58.     '@param {string} Output: File location as string
  59.     '@param {string} Source: Source  to be compiled
  60.     Public Sub Compile(ByVal Source As String)
  61.  
  62.         On Error Resume Next
  63.  
  64.         If Location Is Nothing Then
  65.             Dim result As Integer = MessageBox.Show("Do you want to choose a new location?", "Invalid Location", MessageBoxButtons.YesNo)
  66.             If result = DialogResult.No Then
  67.                 Return
  68.             ElseIf result = DialogResult.Yes Then
  69.                 NewSaveLocation()
  70.             End If
  71.         End If
  72.  
  73.         Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
  74.         Dim Parameters As New CompilerParameters()
  75.         Dim cResults As CompilerResults
  76.  
  77.         Parameters.GenerateExecutable = True
  78.         Parameters.OutputAssembly = Location
  79.  
  80.         For Each Reference In References
  81.             Parameters.ReferencedAssemblies.Add(Reference)
  82.         Next
  83.  
  84.         cResults = Compiler.CompileAssemblyFromSource(Parameters, Source)
  85.  
  86.         If cResults.Errors.Count > 0 Then
  87.             For Each CompilerError In cResults.Errors
  88.                 MessageBox.Show("Error: " & CompilerError.ErrorText, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
  89.             Next
  90.         ElseIf cResults.Errors.Count = 0 Then
  91.  
  92.         End If
  93.  
  94.     End Sub
  95.  
  96.     Public Sub Run()
  97.         If Location Is Nothing Then
  98.             MessageBox.Show("Couldn't find your file", "Invalid File", MessageBoxButtons.OK)
  99.             Return
  100.         End If
  101.         Process.Start(Location)
  102.     End Sub
  103.  
  104.     Public Sub NewSaveLocation()
  105.         Dim sfd As New SaveFileDialog
  106.         sfd.Filter = "Executable *.exe | *.exe"
  107.         ' sfd.Title = "Choose a fiel!"
  108.         sfd.FileName = "Output.exe"
  109.         If sfd.ShowDialog = DialogResult.OK Then
  110.             Location = sfd.FileName
  111.         End If
  112.     End Sub
  113. End Class
Add Comment
Please, Sign In to add comment