Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
I was created the dll using CodeDomProvider,after compile that dll i want delete that dll file, but it's showing Access to the path 'c:\sathish\abc.dll' is denied error.

How to Slove this ??

What I have tried:

my code is

C#
ICodeCompiler compiler = provider.CreateCompiler();
                CompilerParameters parameter = new CompilerParameters();
                string strCompilerfilepath = @"c:\sathish\abc.dll";

                string finalPath = string.Empty;

                parameter.OutputAssembly = finalPath = Path.Combine(strCompilerfilepath, forClassName + ".dll");

                parameter.ReferencedAssemblies.Add("System.dll");
                parameter.ReferencedAssemblies.Add("System.Text.RegularExpressions.dll");
                parameter.GenerateExecutable = false;
                parameter.GenerateInMemory = false;
                parameter.TreatWarningsAsErrors = false;

                CompilerResults results = compiler.CompileAssemblyFromSource(parameter, sb.ToString());
 if (!results.Errors.HasErrors)
 {
	 if (File.Exists(finalPath))
                    {                       
                        File.SetAttributes(finalPath, FileAttributes.Normal);
                        File.Delete(finalPath);
                    }
 }
Posted
Updated 2-Mar-16 22:33pm
Comments
Nathan Minier 26-Feb-16 8:09am    
You may need to kill the process in the task manager.
sv sathish 26-Feb-16 8:12am    
i want kill the process from code
how to do that ???
F-ES Sitecore 26-Feb-16 8:13am    
Something is probably using it so has it locked. Either use common sense to work out what the thing that is using it is, or use something like Process Explorer (google for it) and that'll tell you what is using it.
Afzaal Ahmad Zeeshan 26-Feb-16 8:46am    
If not what F-ES Sitecore helps, I would suggest that you consider not storing the DLLs there. Because mostly, you need to have administrator privileges to perform write actions there.
sv sathish 26-Feb-16 9:56am    
i want to unload the file from assembly.
how to do that
if i do that problem ill solve

I don't know why you want to delete a file that has just been created (and even after changing it's attributes).

Try to use ICodeCompiler.CompileAssemblyFromSource Method (CompilerParameters, String) (System.CodeDom.Compiler)[^] instead. Using CreateCompiler is obsolete.

A probable reason for the denied access might be that the file is still locked while the compiler instance is active.
[EDIT]
When using the obsolete style you can try to call the Component.Dispose Method (Boolean) (System.ComponentModel)[^] to release the unmanaged resources like file handles.
[/EDIT]
 
Share this answer
 
v2
Hi,

The code shown has a mismatch path.
C#
string strCompilerfilepath = @"c:\sathish\abc.dll";
Path.Combine(strCompilerfilepath, forClassName + ".dll");


Your string strCompilerfilepath must be a path to a directory.
The .NET function Path.Combine makes a new path as like this one

C#
c:\sathish\abc.dll\className.dll


If abc.dll is a file and not a directory, you cannot delete your final path.
 
Share this answer
 
v2
Comments
sv sathish 26-Feb-16 9:55am    
sir am editing the path for this post, so this is not a problem.
sorry for that code.
InvisibleMedia 26-Feb-16 13:01pm    
Thanks for your comment. Is this problem remain unsolved ?

If so, there is a little information to know about compiling on the fly;
I explain it myself : depends to switches (Boolean values) in CompileParameters, if the compiled DLL is loaded immediately into the current execution, the DLL remains loaded until the executable quits. In Windows, you cannot easily unload a DLL.
To unload a DLL, there is a workaround : compiles and loads the DLL in a new AppDomain instance.

To do so, AppDomain.CreateDomain() is the main function to create a new domain. You need to create a new evidence and defines in it a new Zone; the zone computer is available; use this zone to address all security limits to the computer (other zones are intranet or internet). After the new domain is created, you can load your compilation process and loads the resulting the DLL. As you are finished, you can call the method Unload() associated with your AppDomain instance. And then, your DLL is then completely unloaded and delete can occur.
But, carefull : communications between two differents domains cannot be synchrone; it's asynchronous and you can transmit some information with and then by Marshalling. Methods in an AppDomain instance GetData() and SetData() are possible; but, in a limited manner, as such a Distributed COM marshal process.
I Solve my Problem.
I create the dll and stored in a bin folder, after I load that assembly to app domain.
now the assembly loaded successfully and unload that app domain.

thank you all your commands.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900