-2

I am making a c# compiler and my code creates an executable that is automatically created in the startup folder(Working Directory). Is there any way to change the path in which the EXE is created before creating it.

This code creates an executable in "C:\Users\User\Desktop\CSharp Compiler\CSharp Compiler\bin\Debug"

        CSharpCodeProvider csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });
        CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, ProjectName.Text + ".exe", true);
       
        parameters.GenerateExecutable = true;
        System.CodeDom.Compiler.TempFileCollection tfc = new TempFileCollection(Application.StartupPath, false);
        CompilerResults cr = csc.CompileAssemblyFromSource(parameters, txtcode.Text);

        if (cr.Errors.HasErrors)
        {
            //Do something if there is an error here...
        }
        else
        {
            //Start the application if there is no errors
            Process.Start(Application.StartupPath + "/" + ProjectName.Text + ".exe");
        }
Simon
  • 27
  • 2
  • 9
  • By copying/moving the exe (and the other assemblies/DLLs/files it depends on) to a different path and start it from there...? Or, what precisely is your question? –  Jan 12 '19 at 23:56
  • the code above takes code from richtextbox and creates an executable from it but it creates it in the Application.StartUpPath how can I let it to creates the executable in a specific path – Simon Jan 13 '19 at 00:02
  • Try setting the output file name including the path. If that doesn't work (i am not sure whether it will work), try setting the output path with file name through CompilerParameters.CompilerOptions according to this answer: https://stackoverflow.com/a/28763396/2819245 –  Jan 13 '19 at 00:10
  • It is a little complicated – Simon Jan 13 '19 at 00:13
  • But you still not changing the path. I will try searching in Microsoft docs maybe there is an answer – Simon Jan 13 '19 at 00:16

1 Answers1

1

To set the working directory, you need to use the ProcessStartInfo version of Process.Start

It can be as simple as

System.Diagnostics.ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo();
PSI.FileName = Application.StartupPath + "/" + ProjectName.Text + ".exe";
PSI.WorkingDirectory = Application.StartupPath;
System.Diagnostics.Process.Start(PSI);
Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23
  • this code that you provide it just starts the exe it doesn't save the exe in another path. no problem at least you tried. – Simon Jan 14 '19 at 03:12
  • 1
    that's not what you asked. You asked how to change where it executes, not where the executable resides. – Garr Godfrey Jan 14 '19 at 10:38