Sign in to follow this  
Subotron

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

Recommended Posts

Subotron    182
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
Subotron    182
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
Metty    122
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this