Gerekli Util kodları

25. Haziran 2009

Tüm coderlerin Util arşivine koyması gereken kod parçacıklarıdır. File, xml, tip dönüşümleri, sesion vs kodlar içinde barınmaktadır. Umarım işinize yarar...

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.SessionState;
using System.Net;
using System.Configuration;
using Func;

namespace Func
{
    public class FuncUtil
    {
        // Methods
        public static bool f_dirControl(string dirPath)
        {
            try
            {
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static bool f_dirDelete(string dirPath)
        {
            try
            {
                if (Directory.Exists(dirPath))
                {
                    Directory.Delete(dirPath, true);
                }
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static bool f_fileDelete(string filePath)
        {
            try
            {
                if (f_fileExists(filePath))
                {
                    File.Delete(filePath);
                }
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static bool f_fileDownload(string URL, string filePath)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.DownloadFile(URL, filePath);
                }
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static bool f_fileExists(string filePath)
        {
            return File.Exists(filePath);
        }

        public static FileInfo f_fileInfo(string filePath)
        {
            return new FileInfo(filePath);
        }

        public static bool f_fileSave(string filePath, string innerText)
        {
            return f_fileSave(filePath, innerText, false);
        }

        public static bool f_fileSave(string filePath, string innerText, bool append)
        {
            return f_fileSave(filePath, innerText, append, "ISO-8859-9");
        }

        public static bool f_fileSave(string filePath, string innerText, bool append, string encoding)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(filePath, append, Encoding.GetEncoding(f_isNull(encoding) ? "ISO-8859-9" : encoding)))
                {
                    writer.Write(innerText);
                }
                return true;
            }
            catch
            {
            }
            return false;
        }

        public static int f_fileSize(string file)
        {
            if (!f_fileExists(file))
            {
                return 0;
            }
            return (int)(f_fileInfo(file).Length / 0x400L);
        }

        public static string f_getAppKey(string key)
        {
            return f_getAppKey(key, "");
        }

        public static string f_getAppKey(string key, string def)
        {
            try
            {
                return ConfigurationManager.AppSettings[key];
            }
            catch
            {
                return def;
            }
        }

        public static string f_getEncFromXml(string xmlText)
        {
            try
            {
                string str = xmlText.Remove(xmlText.IndexOf("?>"), xmlText.Length - xmlText.IndexOf("?>"));
                return str.Remove(0, str.IndexOf("encoding")).Replace("encoding=", "").Replace("\"", "");
            }
            catch
            {
                return "iso-8859-9";
            }
        }

        public static string f_getSessionKey(HttpSessionState SS, string key, string def)
        {
            if (SS[key] == null)
            {
                SS[key] = def;
                return def;
            }
            try
            {
                return (string)SS[key];
            }
            catch
            {
                return def;
            }
        }

        public static bool f_isNull(string NullText)
        {
            if ((!(NullText == string.Empty) && (NullText != null)) && !(NullText == ""))
            {
                return (NullText.Length == 0);
            }
            return true;
        }

        public static string f_loadFile(string filePath)
        {
            return f_loadFile(filePath, "iso-8859-9");
        }

        public static string f_loadFile(string filePath, string encoding)
        {
            try
            {
                using (StreamReader reader = new StreamReader(filePath, Encoding.GetEncoding(encoding)))
                {
                    return reader.ReadToEnd();
                }
            }
            catch
            {
            }
            return "";
        }

        public static string f_loadUrlFile(string url)
        {
            return f_loadUrlFile(url, "iso-8859-9");
        }

        public static string f_loadUrlFile(string url, string encoding)
        {
            try
            {
                HttpWebResponse response = (HttpWebResponse)WebRequest.Create(url).GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)))
                {
                    return reader.ReadToEnd();
                }
            }
            catch
            {
            }
            return "";
        }

        public static byte f_toByte(string text)
        {
            return f_toByte(text, 0);
        }

        public static byte f_toByte(string text, byte def)
        {
            try
            {
                return byte.Parse(text);
            }
            catch
            {
                return def;
            }
        }

        public static DateTime f_toDateTime(string DateTimeText)
        {
            return f_toDateTime(DateTimeText, DateTime.Now);
        }

        public static DateTime f_toDateTime(string DateTimeText, DateTime def)
        {
            try
            {
                return DateTime.Parse(DateTimeText);
            }
            catch
            {
                return def;
            }
        }

        public static double f_toDouble(string text)
        {
            return f_toDouble(text, 0.0);
        }

        public static double f_toDouble(string text, double def)
        {
            try
            {
                return double.Parse(text);
            }
            catch
            {
                return def;
            }
        }

        public static int f_toInt(string IntText)
        {
            return f_toInt(IntText, 0);
        }

        public static int f_toInt(string IntText, int def)
        {
            try
            {
                return int.Parse(IntText);
            }
            catch
            {
                return def;
            }
        }

        public static long f_toLong(string LongText)
        {
            return f_toLong(LongText, 0L);
        }

        public static long f_toLong(string LongText, long def)
        {
            try
            {
                return long.Parse(LongText);
            }
            catch
            {
                return def;
            }
        }
    }
}
 

11 kişi tarafından 4.5 olarak değerlendirildi

  • Currently 4,545455/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Asp.net

Yorumlar

Yorum ekle


(Gravatar simgesini gösterecek)  

  Country flag

biuquote
  • Yorum
  • Canlı önizleme
Loading