Question

I already know how to browse for an image using open file dialog. So let's say we already got the path :

string imagePath = "Desktop/Images/SampleImage.jpg";

I want to copy that file, into my application folder :

string appFolderPath = "SampleApp/Images/";

How to copy the given image to the appFolderPath programmatically?

Thank you.

Was it helpful?

Solution

You could do something like this:

var path = Path.Combine(
    System.AppDomain.CurrentDomain.BaseDirectory,
    "Images",
    fileName);
File.Copy(imagePath, path);

where fileName is the actual name of the file only (including the extension).

UPDATE: the Path.Combine method will cleanly combine strings into a well-formed path. For example, if one of the strings does have a backslash and the other doesn't it won't matter; they are combined appropriately.

The System.AppDomain.CurrentDomain.BaseDirectory, per MSDN, does the following:

Gets the base directory that the assembly resolver uses to probe for assemblies.

That's going to be the executable path you're running in; so the path in the end (and let's assume fileName is test.txt) would be:

{path_to_exe}\Images\test.txt

OTHER TIPS

 string path="Source imagepath";

  File.Copy(System.AppDomain.CurrentDomain.BaseDirectory+"\\Images", path);

\ System.AppDomain.CurrentDomain.BaseDirectory is to provide path of the application folder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top