lordfritte
Erfahrenes Mitglied
Hallo ich versche mir gerade ein kleines BackupProgramm zu schreiben, das soll funktionieren mit einem mehrzeiligen C# Script, so mein erster Test-Programm sieht nun so aus:
Aber mein Problem ist jetzt, wenn er durch mein Script gelaufen ist, wie führe ich es dann aus?
C#:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Collections;
using System.Reflection;
using System.CodeDom;
using Microsoft.CSharp;
namespace MyBackupManager2
{
static class Program
{
static string myScriptFile = "C:\\BackupScript.cs";
static int Main(string[] args)
{
ExecuteScript(myScriptFile);
Console.ReadLine();
return 0;
}
static bool ExecuteScript(string sFile)
{
string sBackupScript = ReadFile(sFile);
if(sBackupScript.Length <= 0)
return false;
try
{
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace nameSpace = new CodeNamespace("BackupManager");
CodeTypeDeclaration relationClass = new CodeTypeDeclaration("BackupClass");
CodeMemberMethod backupFunction = new CodeMemberMethod();
CodeMemberMethod restoreFunction = new CodeMemberMethod();
CompilerResults compilerResults = null;
ArrayList assemblyNames = new ArrayList();
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
if ((asm.Location != null) && (asm.Location.Length > 0))
assemblyNames.Add(new Uri(asm.Location).LocalPath);
}
nameSpace.Types.Add(relationClass);
relationClass.IsClass = true;
relationClass.TypeAttributes = TypeAttributes.Public;
relationClass.Members.Add(backupFunction);
backupFunction.Attributes = MemberAttributes.Public;
backupFunction.ReturnType = new CodeTypeReference(typeof(bool));
backupFunction.Name = "RunBackup";
backupFunction.Parameters.Add(new CodeParameterDeclarationExpression(typeof(BackupClass), "App"));
backupFunction.Statements.Add(new CodeSnippetExpression(sBackupScript));
CompilerParameters compilerParameters = new CompilerParameters((string[])assemblyNames.ToArray(typeof(string)));
compilerParameters.TempFiles = new TempFileCollection(@"C:\Temp", true);
compilerParameters.GenerateInMemory = true;
compileUnit.Namespaces.Add(nameSpace);
CSharpCodeProvider csharp = new Microsoft.CSharp.CSharpCodeProvider();
compilerResults = csharp.CompileAssemblyFromDom(compilerParameters, compileUnit);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
}
return true;
}
static string ReadFile(String sFilename)
{
string sContent = "";
if (File.Exists(sFilename))
{
StreamReader myFile = new StreamReader(sFilename, System.Text.Encoding.Default);
sContent = myFile.ReadToEnd();
myFile.Close();
}
return sContent;
}
}
public class BackupClass
{
public void Test()
{
MessageBox.Show("Hallo");
}
}
}
Aber mein Problem ist jetzt, wenn er durch mein Script gelaufen ist, wie führe ich es dann aus?
Zuletzt bearbeitet von einem Moderator: