[C#].csproj Datei compilen ohne VS

10/19/2011 16:08 .тяµε.#1
Heyho ...
Da ich an einem Programm arbeite, bei dem man eine .exe erstellen soll(in C#), muss ich nun wissen wie man eine - .csproj Datei compilt...
Ich habe schon in Google vieles durchsucht, aber nichts gefunden ..

Ich hab mal was von einer MSBuild gelesen aber leider auch nicht genug gefunden...

Kennt sich da jemand aus? Wäre euch sehr dankbar

LG!
10/19/2011 16:24 XxharCs#2
[Only registered and activated users can see links. Click Here To Register...]
Ist kein Visual Studio aber ein C# Compiler/IDE

mfg
10/19/2011 16:35 .тяµε.#3
Ich hab Visual C#... Ich will durch mein Programm (mit C# erstellt) ein anderes .csproj compilen...
10/19/2011 16:46 XxharCs#4
So wie ich es jetzt verstehe meinst du, das dein Programm ein compiler für dein anderes projekt ist oder ? :OO

Das kommt drauf an wie du dein Programm gemacht hast..Also zB dein Programm ist eine konsolenanwendung und du nicht gecoded hast, das er sich das proket holt,dann kopierst du das projekt und fügst es in dein Programm. (Also das projekt ziehst du auf deine exe,damit dein Projekt compiled wird)

edit: sonst ka was du meinst und wie :O
10/19/2011 17:00 .тяµε.#5
Das haste richtig verstanden ;)

Ich kann es als Konsole oder als Win-Form machen ^^...
Nur habe ich dich jetzt nicht so richtig verstanden o.0
10/19/2011 19:02 boxxiebabee#6
Codedome wäre vll. für dich interessant. Damit kannst du Code dynamisch kompilieren.
10/19/2011 20:16 .тяµε.#7
Quote:
Originally Posted by Lizzaran View Post
Codedome wäre vll. für dich interessant. Damit kannst du Code dynamisch kompilieren.
Ich schau es mir mal an, aber kann man da mehrere Klassen in eine .exe packen und erstellt das nur Konsolenanwendungen?
Aber thx schonmal ;)
10/20/2011 10:40 Demon-777#8
@.тяµε.:

Indeed you may use Codedome! I also have an example for you. So let's create a new console app.
Ofcourse you can use WinForms but console app. suits better here just for now.

Now we need following namespaces added to our imports:
Code:
using System.CodeDom.Compiler;
using Microsoft.CSharp;
I suppose you have a test .cs file you want to compile, if not I wrote one for you.
Just open Notepad and save it as Sample.cs or something:

Code:
using System;

namespace Program
{
    public class MyProgram
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadKey(true);
        }
    }	
}
Now change back to your console app. project and add new method we
need later in order to compile our .cs file.

Code:
/// <summary>
/// Compiles the specified target.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="outputFile">The output file.</param>
public static void Compile(string target, string outputFile)
{
    // Add your references here.
    //
    string[] references = new[] {
        "System.dll"
    };

    var parameters = new CompilerParameters()
    {
        OutputAssembly = outputFile,
        GenerateExecutable = true,
        TreatWarningsAsErrors = true,
        CompilerOptions = "/optimize"
    };

    parameters.ReferencedAssemblies.AddRange(references);

    var compiler = new CSharpCodeProvider();
    var result =
        compiler.CompileAssemblyFromFile(parameters, target);

    if (result.Errors.HasErrors)
    {
        var output = "Failed to compile: ";

        foreach (var error in result.Errors) 
        {
            output += error.ToString();
        }

        throw new ApplicationException(output);
    }
}
Now call it just like this:

Code:
Compile("Sample.cs", "Something.exe");
where Sample.cs your source file and Something.exe your output file.
PS. Don't forget to catch exceptions. ;)

@EDIT:
If you want to compile multiple files then change the target parameter type in my method to string[].

Now call it like this:

Code:
var targets = new[] {
    "Sample.cs",   // Main class that calls all subclasses.
    "Class1.cs",
    "Class2.cs"
};

Compile(targets, "Something.exe");
OR

Code:
Compile(new[] { "Sample.cs", "Class1.cs", "Class2.cs" }, "Something.exe");

Best regards,

Demon-777
10/20/2011 15:05 .тяµε.#9
Thank you!
That helps me a lot :D
With your example everything works perfect.
But with my file, the program has always an error :/ :
[Only registered and activated users can see links. Click Here To Register...]


Source(XNA 4.0 File :awesome:):
Code:
using System;

namespace Program
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using (Game1 game = new Game1())
            {
                game.Run();
            }
        }
    }
}


Game1.cs

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Program
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}



PS: Sry for my bad english





Edit:

~closerequest hab es!
Ein großes Danke an Demon-777, der mir sogar noch per pn geholfen hatte ;) !