本文整理汇总了C#中System.CodeDom.CodeCompileUnit.CompileToCSharpSourceCode方法的典型用法代码示例。如果您正苦于以下问题:C# CodeCompileUnit.CompileToCSharpSourceCode方法的具体用法?C# CodeCompileUnit.CompileToCSharpSourceCode怎么用?C# CodeCompileUnit.CompileToCSharpSourceCode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.CodeDom.CodeCompileUnit
的用法示例。
在下文中一共展示了CodeCompileUnit.CompileToCSharpSourceCode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteFileWithBackups
/// <summary>
/// Writes a file to disk and creates backup copies if a file with the same name already exists
/// </summary>
/// <remarks>Backups are numbered i.e. .1, .2, .3, .4, etc and rotate up as new copies are made (i.e. if .1 exists, it will be renamed .2 and the current one renamed to .1, etc)</remarks>
private void WriteFileWithBackups(string path, CodeCompileUnit code)
{
if (File.Exists(path)) // existing version present; make backups as necessary
{
uint maxBackups = Parameters.MaxBackupCopies;
uint i = maxBackups;
do // find and move existing backups if needed
{
string instanceFileName = path + "." + i.ToString(CultureInfo.InvariantCulture);
bool instanceExists = File.Exists(instanceFileName);
if (i == maxBackups && instanceExists) File.Delete(instanceFileName); // truncate a backup that's too old
if (i > 0 && i != maxBackups && instanceExists) File.Move(instanceFileName, string.Format("{0}.{1}", path, (i + 1).ToString(CultureInfo.InvariantCulture))); // move an existing backup up a number in the backups
i--;
} while (i > 0);
// move the pre-existing file into the file1.ext position
File.Move(path, path + ".1");
}
// write new file
File.WriteAllText(path, code.CompileToCSharpSourceCode());
}