Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!
I want to create a program auto install font for window
I have copy file to %windir%\fonts, so it's accept denie ...
Can not install any font, also file(font)is exist ...
This my code
C++
[DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true)]
        public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
                                         string lpFileName);
        int SumFileInstalled = 0;
try
            {
                string[] filePaths = Directory.GetFiles(textBox1.Text);
                string pathWin = Path.Combine(System.Environment.GetEnvironmentVariable("windir"),"Fonts");
                foreach (string temp in filePaths)
                {
                    try
                    {
                        string fileName = System.IO.Path.GetFileName(temp);
                        string destFile = System.IO.Path.Combine(pathWin, fileName);
                        System.IO.File.Copy(temp, destFile,true);
                        int result = -1;
                        int error = 0;
                        result = AddFontResource(destFile );
                        error = Marshal.GetLastWin32Error();
                        if (result == 0)
                            SumFileInstalled++;
                     }
                    catch (Exception ex)
                    {
                        int result = -1;
                        int error = 0;
                        result = AddFontResource(temp);
                        error = Marshal.GetLastWin32Error();
                        if (result == 0)
                            SumFileInstalled++;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error system, please choose folder default - "+ ex.Message, "Error", MessageBoxButtons.OK);
            }
            MessageBox.Show(SumFileInstalled.ToString()+" : installed successfully.","The finish fonts.",MessageBoxButtons.OK);


plz help me!
i used VS2010, win7 Pro 32bit
Thanks all
Posted
Comments
Sergey Alexandrovich Kryukov 14-Mar-14 23:33pm    
No wonder that access is denied. You need to run application with elevated privileges.
—SA
gggustafson 15-Mar-14 0:11am    
Where did you get your DllImport? I show

[ DllImport ( "gdi32.dll" ) ]
static extern int AddFontResource ( string lpszFilename );

Normal users cannot copy anything to a folder under C:\Windows. You have to be an admin to do that.

If your font is permanent, you have to be an admin to put the font in place and modify the registry to keep the font installed.
 
Share this answer
 
 
Share this answer
 
v2

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