Deleting files after creating them - c#

so what I am creating dll type files, running them and then I want to delete them.
However when I try to delete them I get an exception as it claims they are still in use by another process.
I assuming that the code used to create the files does not dispose of resources correctly to allow the deleting of the file after, here is my code to create the file.
if (!Directory.Exists(PathToRobots + Generation))
{
Directory.CreateDirectory(PathToRobots + Generation);
}
File.WriteAllText(Path.Combine(PathToRobots + Generation, NameSpace + GetRobotName() + robotNumber + ".cs"), code);
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters()
{
GenerateInMemory = false,
GenerateExecutable = false, // True = EXE, False = DLL
IncludeDebugInformation = true,
OutputAssembly = Path.Combine(FileName + ".dll") // Compilation name
};
parameters.ReferencedAssemblies.Add(#"robocode.dll");
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);
if (results.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(sb.ToString());
}
Assembly assembly = results.CompiledAssembly;
provider.Dispose();
The code to delete the file is quite simple and is as follows,
var files = Directory.GetFiles(DirectoryPath);
foreach (var file in files)
{
File.Delete(file);
}
Any idea as to why I can't delete the files?

See the notes at CompilerResults.CompiledAssembly Property
The get accessor for the CompiledAssembly property calls the Load method to load the compiled assembly into the current application domain. After calling the get accessor, the compiled assembly cannot be deleted until the current AppDomain is unloaded.
So when you do this:
Assembly assembly = results.CompiledAssembly;
You have loaded the compiled assembly into the current app domain and thus have locked the file. To be able to delete the generated file, you would need to load it into a separate app domain (this answer may help with specifics for doing that).

Related

CompileAssemblyFromSource returns error "Compiling Expression: cannot open c:\Users\*" for reading 'c:\Users\* is not a valid Win32 resource file

My application generates and compiles code runtime:
CompilerParameters m_cp = new CompilerParameters();
m_cp.ReferencedAssemblies.Add("system.dll");
m_cp.GenerateExecutable = false;
m_cp.GenerateInMemory = true;
m_cp.CompilerOptions = "/optimize";
...
CompilerResults cr = new CSharpCodeProvider().CompileAssemblyFromSource(m_cp, code.ToString());
if (cr.Errors.HasErrors)
{
//getting here with error:
//"Compiling Expression: cannot open c:\Users\*" for reading
//'c:\Users\* is not a valid Win32 resource file
// Example of file c:\Users\[User]\AppData\Local\Temp\1\faw31esr\CSC23CEA88A205E4588B799FD8B4456176B.TMP
}
Issue is happening only for some users but their access is OK (access is the same as for trouble-free users). E.g. problematic users can get to shown directory and remove files.
One of vulnerability management products blocked file access and caused error.

how to query gac for assembly files

I am trying to learn more about Reflection, and took some code already built and added to it. Now I am trying to query the GAC for other assemblies and build type instances, and etc. I modified the code I found here, but myAssemblyList is empty. Can you tell me what I am doing wrong? I debugged and placed a break at "var currentAssembly = value.GetAssembly(f);" and it returns null. All the code I have seen populates Assemblies from the current AppDomain, but I have seen methods like LoadFrom(), which should work with a directory path. I also saw this post, and compiled it.
class Program
{
static void Main(string[] args)
{
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
AppDomain domain = AppDomain.CreateDomain("MyDomain", adevidence, domaininfo);
Type type = typeof(Proxy);
var value = (Proxy)domain.CreateInstanceAndUnwrap(
type.Assembly.FullName,
type.FullName);
//String myDir = "C:\\Windows\\Microsoft.NET\\assembly\\GAC_64\\";
String myDir = "C:\\Windows\\Microsoft.NET\\assembly\\";
List<Assembly> myAssemblyList = new List<Assembly>();
foreach (String f in Directory.GetFiles(myDir, "*.dll", SearchOption.AllDirectories))
{
//Console.WriteLine($"Here is f: {f}");
var currentAssembly = value.GetAssembly(f);
if (currentAssembly != null)
{
myAssemblyList.Add(currentAssembly);
Console.WriteLine(currentAssembly.FullName);
//Console.ReadLine();
}
Console.WriteLine($"Total Assemblies found: {myAssemblyList.Count}");
}
Console.WriteLine($"Total Assemblies found: {myAssemblyList.Count}");
Console.ReadLine();
}
}
public class Proxy : MarshalByRefObject
{
public Assembly GetAssembly(string assemblyPath)
{
try
{
return Assembly.LoadFile(assemblyPath);
}
catch (Exception)
{
return null;
// throw new InvalidOperationException(ex);
}
}
}
I jumped one directory back, and tried to collect from GAC_* i.e. 32, 64, and MSIL. I added a test for null for currentAssembly to address an issue with GetAssembly(). But still some directories that contain dll and non-dll files cause exceptions.
modify this line:
foreach (String f in Directory.GetFiles(myDir))
with
foreach (String f in Directory.GetFiles(myDir, "*.dll", SearchOption.AllDirectories)

Load a usercontrol from a folder programatically

I was looking to load a usercontrol from a folder. I want people to be able to package an extension for my app. To do this, they'll have to create a c# user control and put the designer, code, and resx file into a folder. Then when they want to use their "extension", they'll select a folder from a folder selector (I have that) and my app will load their extension. I want to specifically pull out the user control and cast it into a usercontrol object. Am I able to do this and if so, how?
I looked around the internet and there doesn't really seem to be any close to this question. I wasn't able to create any script that could accomplish this. I don't even know where to start in this one. I know that I have to compile their usercontrol.
If this is not possible, the next best solution I can think of is maybe a precompiled usercontrol. If this is possible, how would I load that?
Any help would be appreciated. Thanks!
If you wish to compile the sources, this can be done using the System.CodeDom. Other than that you should load the types from the assembly and test to see if there is a UserControl in there, load it up and add it to your form.
Here is some example of what i described:
public void LoadPlugin(params string[] sourceCodeFilesForUserControl)
{
// Compile the source files
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.IncludeDebugInformation = true;
parameters.GenerateInMemory = true;
// Add references that they can use
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); // important for UserControl
parameters.TreatWarningsAsErrors = false;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sourceCodeFilesForUserControl);
if (results.Errors.Count > 0)
{
// Handle compile errors
StringBuilder sb = new StringBuilder();
foreach (CompilerError CompErr in results.Errors)
{
sb.AppendLine("Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";");
}
Console.Write(sb.ToString());
}
else
{
// The assembly we can search for a usercontrol
var assembly = results.CompiledAssembly;
// If the assembly was already compiled you might want to load it directly:
// assembly = Assembly.LoadFile(#"C:\Program Files\MyTool\plugins\someplugin.dll");
// Get the first type in the assembly that is a UserControl
var userControl = assembly.GetTypes().FirstOrDefault(x => x.BaseType == typeof(UserControl));
// Create a instance of the UserControl
var createdUserControl = Activator.CreateInstance(userControl, new object[] { }) as UserControl;
// Add the created UserControl to the current form
this.Controls.Add(createdUserControl);
}
}

Shadow copy with AppDomain to overwrite exe at runtime

In the following sample app I create a new AppDomain and I execute it with shadow copy enabled. From the new AppDomain I then try to delete (replace) the original main exe. However I get an "access is denied error". Interestingly, after launching the program, from Windows Explorer it is possible to rename the main exe (but not to delete it).
Can shadow copy work for runtime overwriting of the main exe?
static void Main(string[] args)
{
// enable comments if you wanna try to overwrite the original exe (with a
// copy of itself made in the default AppDomain) instead of deleting it
if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
Console.WriteLine("I'm the default domain");
System.Reflection.Assembly currentAssembly = System.Reflection.Assembly.GetExecutingAssembly();
string startupPath = currentAssembly.Location;
//if (!File.Exists(startupPath + ".copy"))
// File.Copy(startupPath, startupPath + ".copy");
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = Path.GetFileName(startupPath);
setup.ShadowCopyFiles = "true";
AppDomain domain = AppDomain.CreateDomain(setup.ApplicationName, AppDomain.CurrentDomain.Evidence, setup);
domain.SetData("APPPATH", startupPath);
domain.ExecuteAssembly(setup.ApplicationName, args);
return;
}
Console.WriteLine("I'm the created domain");
Console.WriteLine("Replacing main exe. Press any key to continue");
Console.ReadLine();
string mainExePath = (string)AppDomain.CurrentDomain.GetData("APPPATH");
//string copyPath = mainExePath + ".copy";
try
{
File.Delete(mainExePath );
//File.Copy(copyPath, mainExePath );
}
catch (Exception ex)
{
Console.WriteLine("Error! " + ex.Message);
Console.ReadLine();
return;
}
Console.WriteLine("Succesfull!");
Console.ReadLine();
}
You can achive self updating application within a single application with multiple AppDomains. The trick is move application executable to a temporary directory and copy back to your directory, then load the copied executable in a new AppDomain.
static class Program
{
private const string DELETED_FILES_SUBFOLDER = "__delete";
/// <summary>
/// The main entry point for the application.
/// </summary>
[LoaderOptimization(LoaderOptimization.MultiDomainHost)]
[STAThread]
static int Main()
{
// Check if shadow copying is already enabled
if (AppDomain.CurrentDomain.IsDefaultAppDomain())
{
// Get the startup path.
string assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string assemblyDirectory = Path.GetDirectoryName(assemblyPath);
string assemblyFile = Path.GetFileName(assemblyPath);
// Check deleted files folders existance
string deletionDirectory = Path.Combine(assemblyDirectory, DELETED_FILES_SUBFOLDER);
if (Directory.Exists(deletionDirectory))
{
// Delete old files from this folder
foreach (var oldFile in Directory.EnumerateFiles(deletionDirectory, String.Format("{0}_*{1}", Path.GetFileNameWithoutExtension(assemblyFile), Path.GetExtension(assemblyFile))))
{
File.Delete(Path.Combine(deletionDirectory, oldFile));
}
}
else
{
Directory.CreateDirectory(deletionDirectory);
}
// Move the current assembly to the deletion folder.
string movedFileName = String.Format("{0}_{1:yyyyMMddHHmmss}{2}", Path.GetFileNameWithoutExtension(assemblyFile), DateTime.Now, Path.GetExtension(assemblyFile));
string movedFilePath = Path.Combine(assemblyDirectory, DELETED_FILES_SUBFOLDER, movedFileName);
File.Move(assemblyPath, movedFilePath);
// Copy the file back
File.Copy(movedFilePath, assemblyPath);
bool reload = true;
while (reload)
{
// Create the setup for the new domain
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationName = assemblyFile;
setup.ShadowCopyFiles = true.ToString().ToLowerInvariant();
// Create an application domain. Run
AppDomain domain = AppDomain.CreateDomain(setup.ApplicationName, AppDomain.CurrentDomain.Evidence, setup);
// Start application by executing the assembly.
int exitCode = domain.ExecuteAssembly(setup.ApplicationName);
reload = !(exitCode == 0);
AppDomain.Unload(domain);
}
return 2;
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm mainForm = new MainForm();
Application.Run(mainForm);
return mainForm.ExitCode;
}
}
}
As it's an interesting use case of MEF, I've bashed out a quick demo of how to hot-swap running code in C#. This is very simple and leaves out a lot of edge cases.
https://github.com/i-e-b/MefExperiments
Notable classes:
src/PluginWatcher/PluginWatcher.cs -- watches a folder for new implementations of a contract
src/HotSwap.Contracts/IHotSwap.cs -- minimal base contract for a hot-swap
src/HotSwapDemo.App/Program.cs -- Does the live code swap
This doesn't lock the task .dlls in the Plugins folder, so you can delete old versions once new ones are deployed.
Hope that helps.
You asked specifically to use ShadowCopy for the update process. If that (and why would it be?) not a fixed requirement, these ones were real eye openers for me:
https://visualstudiomagazine.com/articles/2017/12/15/replace-running-app.aspx
https://www.codeproject.com/Articles/731954/Simple-Auto-Update-Let-your-application-update-i
It comes down to you renaming the target file (which is allowed, even when it is locked since it is running) and then moving/copying the desired file to the now freed destination.
The vs-magazine article is very detailed, including some nifty tricks like finding out if a file is in use by current application (though only for exe, for .dlls and others one has to come up with a solution).

How can we add embedded resources to a file which is compiled from a source file at run-time

I'm writing a small application which works at compiling a file from a source (.cs) code file using a function:
public static bool CompileExecutable(String sourceName)
{
//Source file that you are compliling
FileInfo sourceFile = new FileInfo(sourceName);
//Create a C# code provider
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
//Create a bool variable for to to use after the complie proccess to see if there are any erros
bool compileOk = false;
//Make a name for the exe
String exeName = String.Format(#"{0}\{1}.exe",
System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_"));
//Creates a variable, cp, to set the complier parameters
CompilerParameters cp = new CompilerParameters();
//You can generate a dll or a exe file, in this case we'll make an exe so we set this to true
cp.GenerateExecutable = true;
//Set the name
cp.OutputAssembly = exeName;
//Save the exe as a physical file
cp.GenerateInMemory = false;
//Set the compliere to not treat warranings as erros
cp.TreatWarningsAsErrors = false;
//Make it compile
CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName);
//if there are more then 0 erros...
if (cr.Errors.Count > 0)
{
//A message box shows the erros that occured
MessageBox.Show("Errors building {0} into {1}" +
sourceName + cr.PathToAssembly);
//for each error that occured in the code make a separete message box
foreach (CompilerError ce in cr.Errors)
{
MessageBox.Show(" {0}" + ce.ToString());
}
}
//if there are no erros...
else
{
//a message box shows compliere results and a success message
MessageBox.Show("Source {0} built into {1} successfully." +
sourceName + cr.PathToAssembly);
}
//if there are erros...
if (cr.Errors.Count > 0)
{
//the bool variable that we made in the beggining is set to flase so the functions returns a false
compileOk = false;
}
//if there are no erros...
else
{
//we are returning a true (success)
compileOk = true;
}
//return the result
return compileOk;
}
What i want to achieve is to add user-selected file resources ( images, mp3, avi, ttf,..etc ) which will be added as embedded resources to the being compiled using the function above.
how can we add embedded resources to a file which is compiled from a source file at run-time ?
You'll need to create a .resources file containing the files you want to embed as resources, and then reference the generated resource file in your CompilerParameter's instance using the EmbeddedResources property.
Follow the directions from Resources in .resources Files section of the first link above (the section that refers to System.Resources.ResourceWriter), which will produce a temporary file. Then based on the code in your question (and the example from the EmbeddedResources documentation), you'll reference it with something like:
if (provider.Supports(GeneratorSupport.Resources))
{
cp.EmbeddedResources.Add("pathToYourGeneratedResourceFile");
}

Categories