简体   繁体   中英

How do I get something from inside a project to a directory? (C#)

Here is my code:

using System;

namespace msoApplier
{
    class Program
    {
        static void Main()
        {
            string targetPath = @"C:\Program Files (x86)\MyProgram";          

            System.IO.Directory.CreateDirectory(targetPath);
            System.IO.File.Copy("ExampleProgram.exe", targetPath);
            Console.ReadKey();
        }
    }
}

ExampleProgram.exe is in the same folder as Program.cs. Is there anything I can do? I don't want ExampleProgram.exe to be seperate from the program, I want it to be inside the program, and then copied to that place.

How about to get the Path of your executing exe? Then you could spezify the copy process with the whole Path?

eg.:

string SourceDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
string SourceFile = SourceDir + "\ExampleProgram.exe";
System.IO.File.Copy(SourceFile , targetPath);

Haven't tested it, because I'm not at Work.

Kind Regards

It sounds like you want to store ExampleProgram.exe inside the program that your source code produces. If that is the case, you can store that ExampleProgram.exe as a resource within your program then read the resource and store it at the location of your choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM