C#
C#

help

Root Question Message

./Jbz
./Jbz12/26/2022
✅ Extracting files that are in your project into a certain directory when compiled into an exe file?

Heya If I had a python file or whatever file in the visual studio file explorer libary https://prnt.sc/5yqXrrYYJrBb

if compiled into an exe or the c# file was ran how would I get the other file into a certain directory. I am new to c#

using System;

namespace Program
{
    class Prog
    {
        public static void Main(string[] args)
        {
            if (!File.Exists(@"C:\Test"))
            {
                Directory.CreateDirectory(@"C:\Test");
                //What I dont know
                ///
                /// The .py file (pyfile.py) gets put into the C:\Test directory
                ////
                ///I believe even if you have multiple files in visual studio it compiles them into 1 exe? Will this effect the .py file getting put into the directory?
            }
        }
    }
}
Ero
Ero12/26/2022
You need to set the files as embedded resources, get their manifest resource stream, and copy that stream to a specified output stream
./Jbz
./Jbz12/26/2022
what?
Ero
Ero12/26/2022
I think i gave you enough things to Google
arion
arion12/26/2022
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);
}
Ero
Ero12/26/2022
Hey yeah let's spoon feed from chatgpt
arion
arion12/26/2022
wasnt from chat gpt though :sadge:
Ero
Ero12/26/2022
Writing out type names with namespaces and using old using statements says otherwise
arion
arion12/26/2022
Well, he's likely a new person to C#, will likely get stuck at "Where is this from, why compile error?"
arion
arion12/26/2022
To avoid that convo, type out the full namespace
arion
arion12/26/2022
same exact thing goes for
arion
arion12/26/2022
resourceNames.FirstOrDefault(name => name.Contains("pyfile.py"));
arion
arion12/26/2022
though if u have that little faith, u do u
Ero
Ero12/26/2022
Or just add it as a using...? You're missing using System.Linq; as well so, completely pointless
Ero
Ero12/26/2022
Just don't spoon feed a complete solution
Ero
Ero12/26/2022
Finding a solution to something as simple as this is an essential skill to learn for beginners
Ero
Ero12/26/2022
You don't learn anything by just copy pasting
arion
arion12/26/2022
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
Ero12/26/2022
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
Ero
Ero12/26/2022
It's really not that hard to Google that. The first result of each of those searches will probably be good enough
arion
arion12/26/2022
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
arion
arion12/26/2022
"How do i do x in c#?"
just google it
Ero
Ero12/26/2022
There are things that warrant that. This is one of those things
arion
arion12/26/2022
I'll agree to disagree
arion
arion12/26/2022
:poi_shrug:
./Jbz
./Jbz12/26/2022
tyyy
./Jbz
./Jbz12/26/2022
where does this copy the file too
arion
arion12/26/2022
new System.IO.FileStream("pyfile.py"...

if you dont specify an absolute path, it uses a relative path
which is the Working Directory iirc
./Jbz
./Jbz12/26/2022
👍
./Jbz
./Jbz12/26/2022
thanks for sticking up for me ill try and learn a bit more csharp before
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy