Beispiel #1
0
        public MemIniFile(string fileName, bool loadDirectly, bool readOnly)
        {
            this.fileName = fileName;
            try
            {
                if (FileOperations.FileExists(fileName))
                {
                    encoding = NativeMethods.GetFileEncoding(fileName);
                }
                else
                {
                    NativeMethods.CreateUnicodeFile(fileName);
                    encoding = Encoding.Unicode;
                }
            }
            catch
            {
                //if we can't create unicode file, lets use ASCII version
                encoding = Encoding.ASCII;
            }

            this.ReadOnly = readOnly;
            if (loadDirectly)
            {
                Load();
            }
        }
Beispiel #2
0
        public static string CustomFileIcon(string fileName)
        {
            if (string.IsNullOrEmpty(fileName) || (appImages == null))
            {
                return(null);
            }

            string imageName = null;

            if (FileOperations.FileIsLink(fileName))
            {
                string tempName = FileOperations.ExtractFileNameFromShellLink(fileName);
                if (!string.IsNullOrEmpty(tempName))
                {
                    fileName = tempName;
                }
            }

            string shortName = Path.GetFileName(fileName);

            imageName = appImages.ReadString("Program", shortName);
            if (!string.IsNullOrEmpty(imageName))
            {
                if (FileOperations.FileExists(imageName))
                {
                    return(imageName);
                }
            }
            return(null);
        }
Beispiel #3
0
        private unsafe void Load()
        {
            if (!FileOperations.FileExists(fileName))
            {
                return;
            }
            nativeMemory = NativeMethods.FileReadToBuffer(fileName);
            memBytePtr   = (byte *)nativeMemory.ToPointer();
#if PRESSURE
            long pressure = InteropHelper.AlignToPage(fileSize);
            if (pressure != 0L)
            {
                GC.AddMemoryPressure(pressure);
            }
#endif
        }
Beispiel #4
0
        /// <summary>
        /// Customs the site icon name.
        /// </summary>
        /// <param name="siteAddress">The site address.</param>
        /// <returns></returns>
        public static string CustomSiteIcon(string siteAddress)
        {
            if (File.Exists(GlobalConfig.SiteImagesFileName))
            {
                Uri        siteUri   = new Uri(siteAddress);
                string     imageName = null;
                string     siteHost  = siteUri.Host;
                string[]   parts     = siteHost.Split('.');
                MemIniFile iniFile   = new MemIniFile(GlobalConfig.SiteImagesFileName);
                try
                {
                    iniFile.Load();
                    foreach (var part in parts)
                    {
                        imageName = iniFile.ReadString("site", part);
                        if (!string.IsNullOrEmpty(imageName))
                        {
                            if (FileOperations.FileExists(imageName))
                            {
                                //return FileOperations.StripFileName(imageName);
                                return(imageName);
                            }
                        }
                    }

                    //no image for url part, return default image
                    if (iniFile.SectionExists("default"))
                    {
                        imageName = iniFile.ReadString("default", "url");
                        if (!string.IsNullOrEmpty(imageName))
                        {
                            if (FileOperations.FileExists(imageName))
                            {
                                //return FileOperations.StripFileName(imageName);
                                return(imageName);
                            }
                        }
                    }
                }
                finally
                {
                    iniFile.Dispose();
                }
            }

            return(null);
        }
Beispiel #5
0
        public static string GetSkinCaption(string skinIniFile)
        {
            string result = null;

            if (string.IsNullOrEmpty(skinIniFile))
            {
                return(null);
            }

            string fullSkinIniFile = FileOperations.StripFileName(skinIniFile);

            if (FileOperations.FileExists(fullSkinIniFile))
            {
                string defaultCaption = Path.GetFileNameWithoutExtension(Path.GetDirectoryName(fullSkinIniFile));
                result = NativeMethods.ReadString(fullSkinIniFile, "Krento", "Caption", defaultCaption);
                return(result);
            }
            else
            {
                return(null);
            }
        }
Beispiel #6
0
        public static Bitmap FromFile(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return(null);
            }

            if (FileOperations.FileExists(filename))
            {
                if (FileOperations.FileExtensionIs(filename, ".png"))
                {
                    int    w = 0;
                    int    h = 0;
                    IntPtr ppvBits;
                    IntPtr hBitmap = NativeMethods.LoadBitmapPNG(filename, ref w, ref h, out ppvBits);
                    if (hBitmap != IntPtr.Zero)
                    {
                        Bitmap     result = new Bitmap(w, h, PixelFormat.Format32bppArgb);
                        BitmapData data   = result.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                        NativeMethods.RtlMoveMemory(data.Scan0, ppvBits, h * data.Stride); // copies the bitmap
                        result.UnlockBits(data);
                        NativeMethods.DeleteObject(hBitmap);
                        result = BitmapPainter.ConvertTo32Bit(result, true);
                        return(result);
                    }
                }

                if (FileOperations.FileExtensionIs(filename, ".jpg") || FileOperations.FileExtensionIs(filename, ".jpeg"))
                {
                    int    w = 0;
                    int    h = 0;
                    IntPtr ppvBits;
                    IntPtr hBitmap = NativeMethods.LoadBitmapJPG(filename, ref w, ref h, out ppvBits);
                    if (hBitmap != IntPtr.Zero)
                    {
                        Bitmap     result = new Bitmap(w, h, PixelFormat.Format24bppRgb);
                        BitmapData data   = result.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                        NativeMethods.RtlMoveMemory(data.Scan0, ppvBits, h * data.Stride); // copies the bitmap
                        result.UnlockBits(data);
                        NativeMethods.DeleteObject(hBitmap);
                        Bitmap realResult = BitmapPainter.ConvertTo32Bit(result, true);
                        return(realResult);
                    }
                }

                if (FileOperations.FileExtensionIs(filename, ".ico"))
                {
                    Icon   icon   = new Icon(FileOperations.StripFileName(filename));
                    Bitmap result = icon.ToBitmap();
                    icon.Dispose();
                    return(result);
                }

                Bitmap image = null;
                IntPtr zero  = IntPtr.Zero;
                int    imageType;

                if (NativeMethods.GdipLoadImageFromFile(FileOperations.StripFileName(filename), out zero) != 0)
                {
                    return(null);
                }

                if (NativeMethods.GdipGetImageType(zero, out imageType) != 0)
                {
                    return(null);
                }

                if (imageType == 1)
                {
                    image = (Bitmap)typeof(System.Drawing.Bitmap).InvokeMember("FromGDIplus", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[] { zero });
                }

                if (!NativeMethods.IsAnimatedGIF(filename))
                {
                    image = BitmapPainter.ConvertTo32Bit(image, true);
                }

                return(image);
            }
            else
            {
                return(null);
            }
        }
Beispiel #7
0
        public void Merge(string extraFile)
        {
            string s;
            string currentSection = null;

            string[] content;
            Encoding extraEncoding;

            if (string.IsNullOrEmpty(extraFile))
            {
                return;
            }

            if (!FileOperations.FileExists(extraFile))
            {
                return;
            }

            extraEncoding = NativeMethods.GetFileEncoding(extraFile);

            using (StreamReader reader = new StreamReader(extraFile, extraEncoding, false, 512))
            {
                while ((s = reader.ReadLine()) != null)
                {
                    //s = s.Trim();

                    if (string.IsNullOrEmpty(s))
                    {
                        continue;
                    }

                    if (s[0] == ';')
                    {
                        continue;
                    }

                    if ((s[0] == openSection) && (s[s.Length - 1] == closeSection))
                    {
                        currentSection = s.Substring(1, s.Length - 2);
                        sections.Add(currentSection);
                    }
                    else
                    {
                        if (currentSection == null)
                        {
                            continue;
                        }

                        int j = s.IndexOf(divider);
                        if (j > 0)
                        {
                            content = s.Split(new char[1] {
                                divider
                            }, 2);
                            if (content.Length == 2)
                            {
                                sections[currentSection].Add(content[0].Trim(), content[1].Trim());
                            }
                        }
                    }
                }
            }
        }
Beispiel #8
0
        public unsafe void Load()
        {
            string s;
            string currentSection = null;

            string[] content;


            sections.Clear();

            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            if (!FileOperations.FileExists(fileName))
            {
                return;
            }

            TextFileReader reader = null;

            try
            {
                try
                {
                    reader = new TextFileReader(fileName, encoding);
                    while ((s = reader.ReadLine()) != null)
                    {
                        if (string.IsNullOrEmpty(s))
                        {
                            continue;
                        }

                        if (s[0] == ';')
                        {
                            continue;
                        }

                        if ((s[0] == openSection) && (s[s.Length - 1] == closeSection))
                        {
                            currentSection = s.Substring(1, s.Length - 2);
                            sections.Add(currentSection);
                        }
                        else
                        {
                            if (currentSection == null)
                            {
                                continue;
                            }

                            int j = s.IndexOf(divider);
                            if (j > 0)
                            {
                                content = s.Split(new char[1] {
                                    divider
                                }, 2);
                                if (content.Length == 2)
                                {
                                    sections[currentSection].Add(content[0].Trim(), content[1].Trim());
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    TraceDebug.Trace("MemIniFile.Load: " + ex.Message);
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }