Getting the path for the program data folder

Getting the path for the program data folder

stever66
Advisor Advisor
3,337 Views
5 Replies
Message 1 of 6

Getting the path for the program data folder

stever66
Advisor
Advisor

I need to load an image file from the program data folder, like this:

 

Uri uriImage = new Uri(@"C:\ProgramData\Autodesk\ApplicationPlugins\\MyIcon32x32.png");

 

But on different computers, the folder may be on the "D:\" drive, or any other drive letter.

 

How do I rewrite this line so it works on any computer? 

 

Since my .dll file should be in the same folder, I tried using @".\MyIcon32x32.png".   I also tried just "MyIcon32x32.png", but neither worked. 

 

 

0 Likes
Accepted solutions (1)
3,338 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

 

Dear stever,

 

 

As i know best way to keep icons file into embeded resource. To do this you can follow below instruction.

 

 

 

Step1.png

 

 

To Show as icon: You Can use following Code:

 

Step 2:

 

 static BitmapSource GetEmbeddedImage(string name)
        {
            try
            {
                Assembly a = Assembly.GetExecutingAssembly();
                Stream s = a.GetManifestResourceStream(name);
                return BitmapFrame.Create(s);
            }
            catch
            {
                return null;
            }
        }



PushButtonData pbdBop = new PushButtonData("pbdBop", @"B.O.P", assemFullName, "AddIns2014.BOP"); PushButton pbBop = panel.AddItem(pbdBop) as PushButton; pbBop.ToolTip = @" Click to get BOP"; pbBop.LargeImage = GetEmbeddedImage("AddIns2014.Images.BOPNew.ico"); panel.AddSeparator();

 

 

 

Message 3 of 6

Dale.Bartlett
Collaborator
Collaborator

If you want the location of the executing dll, I have been using this for some time (not original):

// using System.Reflection;
        // return dll locations
        public static string GetDLLFolder()
        {
            System.Reflection.Assembly Asmbly = null;
            string lstrDllLocation = string.Empty;
            // get current dll name
            string lstrDllName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

            foreach (System.Reflection.Assembly Asmbly_loopVariable in System.AppDomain.CurrentDomain.GetAssemblies())
            {
                Asmbly = Asmbly_loopVariable;
                lstrDllLocation = Asmbly.Location.ToString();

                string lstrDllNameTest = Path.GetFileNameWithoutExtension(lstrDllLocation);

                if (lstrDllName.ToUpper() == lstrDllNameTest.ToUpper())
                {               
                    break;
                }
            }
            return lstrDllLocation;
        }



______________
Yes, I'm Satoshi.
Message 4 of 6

Anonymous
Not applicable
Accepted solution

Very Simple

 

String ProgramDataFolder= Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

 

Enjoy

 

Thanks & Regards

Sanjay Pandey

0 Likes
Message 5 of 6

stever66
Advisor
Advisor

Thanks for 3 excellent suggestions.

 

I'll have to look into the embeded resources a little more.   Does that embed the image file in the .dll file, or is it in another file?   The .dll file is the only file (besides the addin file and the image file) that gets transfered to other computers.  I'm not moving the entire VS solution folder to other computers. 

 

Dale:  I'll have to try that code section sometime.   It looks like it has the ability to return multiple paths (maybe if there are different addins in different directories?)

 

Sanjay's solution worked for me.  I was actually using something similar to find the AppData path.  I tried to modify it by changing "AppData" to "ProgramData", but that wasn't a valid keyword.  I didn't realize "CommonApplicationData" was the keyword I was looking for.

 

 

0 Likes
Message 6 of 6

Dale.Bartlett
Collaborator
Collaborator
Just keep in mind that Environment.SpecialFold​er.CommonApplicationData is not necessarily the executing dll's folder.



______________
Yes, I'm Satoshi.
0 Likes