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:
Code csharp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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?