Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to Copy file from my project Location to local Disk[i.e. on "D" drive]

My Solution Name --> CoffeeShop I add existing item to my solution--FileName--Logo.JPG

I Just want to copy this Logo.JPG file from my project/solution location to Local D Disk.

I have an Idea about File.Copy() method.

But how i can set location of Logo.JPG file as from Location.

e.g.

string solutionFileLocation = [how I can get this location?]
string to_Location= @"D:\";

File.Copy(solutionFileLocation,to_Location);
Thnx in advance
Posted
Comments
F. Xaver 7-Aug-15 4:50am    
Your question isn't clear...
what do you want..
copy a file form your solution to another place? -> why not simply using File-Explorer!?
copy a file from your RUNNING App current directory -> System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) or Application.StartupPath
if you want that file to be in your build directory -> look at Properties->Advanced->Copy to Output Directory

Your question does not really make any sense. If you need to call File.Copy from certain directory to some other directory, you do it during runtime. And during runtime, there is no such thing as solution, project, as well as any other source file. This is what goes to the build, and the build produced modules and assemblies.

If you simply need to know the executable directory, the directory where your application assembly (assemblies, more exactly, assemblies' executable modules) are located, please see my past answer: How to find my programs directory (executable directory).

See also my other answer: How to find my programs directory (current directory, "special folders").

—SA
 
Share this answer
 
v2
Hi If u add images likes project propertis rigth click --> Add ->Existing item,then use below code to get image file path

C#
private void button1_Click(object sender, EventArgs e)
     {
   string strpath = Application.StartupPath; //in this strpath u get application directory path,i mean ur solution path
         strpath = strpath + "\\Logo.JPG"; // here u can add then file name 
         MessageBox .Show (strpath ); //and finally u get full image file path

     }


If u save file inside any folder then use this

C#
{
  string strpath = Application.StartupPath; //in this strpath u get application directory path,i mean ur solution path
        strpath = strpath + "\\NF\\Logo.JPG"; // here u can add then file name,NF is folder name inside solution
        MessageBox .Show (strpath ); //and finally u get full image file path

    }
 
Share this answer
 
C#
string solutionFileLocation
= Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName+"\\CoffeeShop\\Logo.JPG";
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Aug-15 1:43am    
Sorry, this answer is totally wrong. Current directory is not a property of an application, assembly, and not anything fixed. This is the directory used to start the application, which can be changed in the application. The user can make this directory absolutely anything.

This is not even related to programming much. This is what every advanced user should know, even non-engineer.

I'll answer...

—SA
sasanka sekhar panda 7-Aug-15 2:12am    
Thank u for your valuable comment..Please read the question carefully..Not at all agreed with you.
Sergey Alexandrovich Kryukov 7-Aug-15 2:26am    
If you read it carefully, you will understand that the question is self-contradicting. But it cannot make your answer correct. If you try to bring some sense to the question, it won't be related to current directory. For those who also don't have elementary knowledge at the level of the basic user, I'll repeat in extra detail:

Let's say, you run your fragment of code anywhere. If will work for one start directory and won't work for another one, that is, Logo.jpg won't be found in general case, no matter where it really is. It's the user who decides where to start the application.

Thank you for your understanding. If you did not get it, your follow-up questions will be welcome.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900