當前位置: 首頁>>代碼示例>>C#>>正文


C# VBCodeProvider類代碼示例

本文整理匯總了C#中Microsoft.VisualBasic.VBCodeProvider的典型用法代碼示例。如果您正苦於以下問題:C# VBCodeProvider類的具體用法?C# VBCodeProvider怎麽用?C# VBCodeProvider使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


VBCodeProvider類屬於Microsoft.VisualBasic命名空間,在下文中一共展示了VBCodeProvider類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CompileExecutable

public static bool CompileExecutable(String sourceName)
       {
           FileInfo sourceFile = new FileInfo(sourceName);
           CodeDomProvider provider = null;
           bool compileOk = false;

           // Select the code provider based on the input file extension.
           if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
           {
               provider = CodeDomProvider.CreateProvider("CSharp");
           }
           else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
           {
               provider = CodeDomProvider.CreateProvider("VisualBasic");
           }
           else 
           {
               Console.WriteLine("Source file must have a .cs or .vb extension");
           }

           if (provider != null)
           {

               // Format the executable file name.
               // Build the output assembly path using the current directory
               // and <source>_cs.exe or <source>_vb.exe.

               String exeName = String.Format(@"{0}\{1}.exe", 
                   System.Environment.CurrentDirectory, 
                   sourceFile.Name.Replace(".", "_"));

               CompilerParameters cp = new CompilerParameters();

               // Generate an executable instead of 
               // a class library.
               cp.GenerateExecutable = true;

               // Specify the assembly file name to generate.
               cp.OutputAssembly = exeName;
   
               // Save the assembly as a physical file.
               cp.GenerateInMemory = false;
   
               // Set whether to treat all warnings as errors.
               cp.TreatWarningsAsErrors = false;

               // Invoke compilation of the source file.
               CompilerResults cr = provider.CompileAssemblyFromFile(cp, 
                   sourceName);
   
               if(cr.Errors.Count > 0)
               {
                   // Display compilation errors.
                   Console.WriteLine("Errors building {0} into {1}",  
                       sourceName, cr.PathToAssembly);
                   foreach(CompilerError ce in cr.Errors)
                   {
                       Console.WriteLine("  {0}", ce.ToString());
                       Console.WriteLine();
                   }
               }
               else
               {
                   // Display a successful compilation message.
                   Console.WriteLine("Source {0} built into {1} successfully.",
                       sourceName, cr.PathToAssembly);
               }
             
               // Return the results of the compilation.
               if (cr.Errors.Count > 0)
               {
                   compileOk = false;
               }
               else 
               {
                   compileOk = true;
               }
           }
           return compileOk;
       }
開發者ID:.NET開發者,項目名稱:Microsoft.VisualBasic,代碼行數:80,代碼來源:VBCodeProvider


注:本文中的Microsoft.VisualBasic.VBCodeProvider類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。