Hack Forums
[C#] [C#] Mono - How do I delete resource file(s) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [C#] Mono - How do I delete resource file(s) (/showthread.php?tid=6116015)



[C#] Mono - How do I delete resource file(s) - Grooty - Oct 16, 2020

How to delete resource from name?

Code
public static void RemoveResource(AssemblyDefinition asmDefinition)
{
  foreach (var v in asmDefinition.MainModule.Resources.OfType<EmbeddedResource>()) // result: Mono.Cecil.EmbeddedResource
  {
    Console.WriteLine(v);

    int index = asmDefinition.MainModule.Resources.IndexOf(v); // result: 0 1
    Console.WriteLine(index);
    asmDefinition.MainModule.Resources.RemoveAt(index); // Delete
  }
}

1 - Doesn't delete all resources by index.
2 - How to delete resource files by resource file name?

The resources contain files:

FileText.txt, MusicList.wav

I need to delete resources by name namely: FileText and MusicList


RE: [C#] Mono - How do I delete resource file(s) - Lookdadon - Aug 18, 2024

Well i didnt understand your couples of first lines but what you want to do is using txt file and wav file and then delete them after?

its sample but tricky:
be sure to dispose correctly these resources:
you can use IDisposable interface and implementing a Dispose function or in the case of files you can use them inside a using block
Code
using(StreamReader sr = new StreamReader(path/to/your/txtfile.txt)
{
var x = sr.ReadToEnd();
}
i prefer using a using block because once code jump out of the using the resource is automatically disposed
or
Code
StreamReader sr = new StreamReader(path/to/your/txtfile.txt)
var x = sr.ReadToEnd();
sr.Close();
there is alot of way to dispose

then
Code
if(File.Exist(path/to/your/file)
{
File.Delete(path/to/your/file);
}