0

I am trying to create a file path, at the end of which will be a "images" folder that the program will use with the Image command to load jpeg files. I want the program to dynamically know to load the jpeg files from the image folder where ever the executable is launched. I was able to find this on this site, I added the 2 bottom code lines:

public static string AssemblyDirectory
    {
        get
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return System.IO.Path.GetDirectoryName(path);
        }
    }

    public static string imagePath = @"\images";
    public static string finalImagePath = AssemblyDirectory + imagePath;

If I set a break point, the 'finalImagePath' is: "C:\Users\My Name\Documents\Visual Studio 2010\Projects\Universal Serial Diagnostics\bin\Debug\images"

Which is correct, but how do I incorporate that with:

     Image image = Image.FromFile(@"C:\Users\My Name\Desktop\Dip\ENV500008.jpg");

Replacing the hard coded path with the dynamic path. The ENV500008.jpg would be stored in the images folder. Thank you.

Daniel Steele
  • 33
  • 1
  • 1
  • 7
  • I believe that you are looking for Path.GetFileName; https://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx. And then concatenate with finalImagePath. – JamieMeyer Jan 14 '17 at 01:57
  • The top code gets the path, how do I replace the variable "finalImagePath" and use that with the Image.FromFile command? – Daniel Steele Jan 14 '17 at 01:59
  • Image image = Image.FromFile(@"C:\Users\My Name\Desktop\Dip\ENV500008.jpg"); – Daniel Steele Jan 14 '17 at 02:00
  • http://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path – Slai Jan 14 '17 at 02:02
  • Do you want it to be `C:\Users\My Name\Documents\Visual Studio 2010\Projects\Universal Serial Diagnostics\bin\Debug\images\ENV500008.jpg`? – Ghasan غسان Jan 14 '17 at 02:02
  • 2
    It is not clear what you are trying to achieve. What is the role of the path contained in Image.FromFile....? – JamieMeyer Jan 14 '17 at 02:03
  • I figured it out, this is what I was looking for: – Daniel Steele Jan 14 '17 at 02:12
  • Image image = Image.FromFile(@finalImagePath+"\\ENV500008.jpg"); – Daniel Steele Jan 14 '17 at 02:12
  • Basically, I want an Images folder (contains just JPEG files) that are stored in same direectory as the .EXE file. So If a user moves the EXE file and the image folder to another directory, the program dynamically updates the path to the Images folder. Does that make any sense? – Daniel Steele Jan 14 '17 at 02:15
  • or you can just add the image as resource in the executable https://www.youtube.com/watch?v=LQMDsJgMXhE – Slai Jan 14 '17 at 02:34

3 Answers3

1
string path = AppDomain.CurrentDomain.BaseDirectory + "ENV500008.jpg";

if (System.IO.File.Exists(path))
{
    Image image = Image.FromFile(path);
    // .. the rest of the code that uses the image .. 
}
Slai
  • 22,144
  • 5
  • 45
  • 53
0
       Image image = Image.FromFile(@finalImagePath + "\\ENV500008.jpg");

        Image image5 = Image.FromFile(@finalImagePath + "\\ENV510829-2.jpg");
Daniel Steele
  • 33
  • 1
  • 1
  • 7
  • May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. – RBT Jan 14 '17 at 11:41
  • OK, 1) I was trying to dynamically find the path to my EXE file. Which I found the answer to on this website. 2) Once I know the path, I created an 'images' folder that has about 100 JPEG files. Where I was having difficulty was in the use of the IMAGE image - Image.FromFile(path) code. So I posted the above question. Meanwhile I kept playing around with it. I finally answered my own question. I was trying to make my code work in a way that if a user moved the EXE file and images folder, the program would still work. – Daniel Steele Jan 14 '17 at 15:03
  • I have since moved to VS2015 and am now embedding the JPEG files as a resource, larger file size of the exe, but I don't have to worry about users moving the JPEG files. Thanks for the help! – Daniel Steele Jan 14 '17 at 20:09
  • Please add all this information into the post itself and not just as comments. Details in post is more helpful. – RBT Jan 14 '17 at 22:15
0

public static string GetAnyPath(string fileName)
{
            //my path where i want my file to be created is : "C:\\Users\\{my-system-name}\\Desktop\\Me\\create-file\\CreateFile\\CreateFile\\FilesPosition\\firstjson.json"
            var basePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath.Split(new string[] { "\\CreateFile" }, StringSplitOptions.None)[0];
            var filePath = Path.Combine(basePath, $"CreateFile\\CreateFile\\FilesPosition\\{fileName}.json");
            return filePath;
}

change .json to any type according to need refer :https://github.com/swinalkm/create-file/tree/main/CreateFile/CreateFile

Swinal
  • 1