✅ Extracting files that are in your project into a certain directory when compiled into an exe file?

17 Replies
ero
ero3y ago
You need to set the files as embedded resources, get their manifest resource stream, and copy that stream to a specified output stream
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
ero
ero3y ago
I think i gave you enough things to Google
arion
arion3y ago
pyfile.py -> Right Click -> Properties -> Build Action: "Embedded resource"
string[] resourceNames = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

string yourLikelyPythonFilePath = resourceNames.FirstOrDefault(name => name.Contains("pyfile.py"));

if (yourLikelyPythonFilePath == null)
{
throw new Exception("Could not find the python file in the resources");
}

System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(yourLikelyPythonFilePath);

using (System.IO.FileStream fileStream = new System.IO.FileStream("pyfile.py", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
stream.CopyTo(fileStream);
}
string[] resourceNames = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();

string yourLikelyPythonFilePath = resourceNames.FirstOrDefault(name => name.Contains("pyfile.py"));

if (yourLikelyPythonFilePath == null)
{
throw new Exception("Could not find the python file in the resources");
}

System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(yourLikelyPythonFilePath);

using (System.IO.FileStream fileStream = new System.IO.FileStream("pyfile.py", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
stream.CopyTo(fileStream);
}
ero
ero3y ago
Hey yeah let's spoon feed from chatgpt
arion
arion3y ago
wasnt from chat gpt though sadge
ero
ero3y ago
Writing out type names with namespaces and using old using statements says otherwise
arion
arion3y ago
Well, he's likely a new person to C#, will likely get stuck at "Where is this from, why compile error?" To avoid that convo, type out the full namespace same exact thing goes for
resourceNames.FirstOrDefault(name => name.Contains("pyfile.py"));
resourceNames.FirstOrDefault(name => name.Contains("pyfile.py"));
though if u have that little faith, u do u
ero
ero3y ago
Or just add it as a using...? You're missing using System.Linq; as well so, completely pointless Just don't spoon feed a complete solution Finding a solution to something as simple as this is an essential skill to learn for beginners You don't learn anything by just copy pasting
arion
arion3y ago
Not many ppl care about reflection enough to go read 10 docs pages about it and 5 yt videos to complete a simple thing
ero
ero3y ago
It's like 3 Google searches. How to set it as an embedded resource, how to get the resource stream (i literally even spelled it out so they can search it easier), and how to copy it to an output stream It's really not that hard to Google that. The first result of each of those searches will probably be good enough
arion
arion3y ago
You wouldnt be giving much help when u just say "Just google it" though, at that point i would actually just recommend to sign up for openai and ask chatgpt everything "How do i do x in c#?" just google it
ero
ero3y ago
There are things that warrant that. This is one of those things
arion
arion3y ago
I'll agree to disagree <:poi_shrug:582941309174546569>
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
arion
arion3y ago
new System.IO.FileStream("pyfile.py"...
new System.IO.FileStream("pyfile.py"...
if you dont specify an absolute path, it uses a relative path which is the Working Directory iirc
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?