当前位置: 首页>>代码示例>>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;未经允许,请勿转载。