Jump to content
  • Advertisement
Sign in to follow this  
Subotron

[.net] CompileAssemblyFromFile: Load scripts from non-working directory

This topic is 3716 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm implementing C# scripting in my framework, and it all works fine, but I have one problem I'm unable to solve: In my LoadScript( string fileName ) function, I execute this line of code: CompilerResults results = codeCompiler.CompileAssemblyFromFile( parameters, fileName ); parameters looks like this: CompilerParameters parameters = new CompilerParameters(); parameters.GenerateExecutable = false; parameters.GenerateInMemory = true; parameters.IncludeDebugInformation = false; parameters.TreatWarningsAsErrors = false; parameters.ReferencedAssemblies.Add( "System.dll" ); parameters.ReferencedAssemblies.Add( "System.Windows.Forms.dll" ); It works fine if fileName points to a file in the current (working) directory, so "Myscript.cs" works fine. However, when I point to a script in another directory, things get messed up. If the script is located in /Bin/Scripts (Bin being the working dir), I cannot seem to call the script. fileName = "Myscript.cs" results in an IO.FileNotFound exception (doh) fileName = "Scripts/Myscript.cs" does not generate this exception, but instead the results indicate 1 error in the script, which is (translated to English) "error CS1504: Source file 'blablabla\Bin\Myscript.cs' could not be opened ('The system cannot find the specified file. ')} object {System.CodeDom.Compiler.CompilerError}" So now suddenly it appears the compiler is looking for the script inside the working directory, even though I specified a different file name which didn't generate a IO.FileNotFound exception. I can't find out why this is happening, nor have I been able to find a solution using Google and such. Anyone have a clue why this is happening and how I fix it?

Share this post


Link to post
Share on other sites
Advertisement
To add: It works fine if I place Myscript.cs in both /Bin and /Bin/Scripts, but this is obviously not something I want to do.

Share this post


Link to post
Share on other sites
a) Try using Absolute Path's (Path.GetAbsolutePath or sth like that)
b) Change the working directory temporarily? (Environment.CurrentDirectory = ... or sth like that)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!