Friday, May 1, 2015

asp.net Trim Zero

  public static string TrimZero(string byval)
        {
            string result = byval;
            int conertResult = 0;

            try
            {
                conertResult = Convert.ToInt32(byval);
                result = conertResult.ToString();
            }
            catch
            {
                return result;
            }

            return result;
        }

asp.net Copy Folder

  public static Boolean CopyFolder(string existingDocPath, string uploadDocPath)
        {
            bool isResult = false;
            try
            {
                string _targetPath = Convert.ToString(Config.Get("UploadDocPath"));

                DirectoryInfo dir1 = new DirectoryInfo(_targetPath + existingDocPath);

                if (Directory.Exists(_targetPath + existingDocPath) && Directory.Exists(_targetPath + uploadDocPath))
                {
                    FileInfo[] Folder1Files = dir1.GetFiles();
                    if (Folder1Files.Length > 0)
                    {
                        foreach (FileInfo aFile in Folder1Files)
                        {
                            if (!File.Exists(_targetPath + uploadDocPath + "\\" + aFile.Name))
                            {
                                aFile.CopyTo(_targetPath + uploadDocPath + "\\" + aFile.Name);
                            }
                        }
                    }
                    isResult = true;
                }
                else
                {
                    isResult = false;
                }
                   
            }
            catch (BaseException ex)
            {
                logger.LogError("Library.Functions.CopyFolder(): Exception: " + ex.ToString());
            }

            return isResult;
        }

asp.net create directory

  public static Boolean CreateDirectory(string _targetFolder)
        {
            try
            {
                if (!Directory.Exists(_targetFolder))
                {
                    Directory.CreateDirectory(_targetFolder);
                }
                return true;
            }
            catch
            {
                throw;
                return false;
            }
        }

asp.net Get Xml String

 public static string GetXmlString(object xmlClassObject)      
        {
       
            try
            {
                XmlSerializer serializer = new XmlSerializer(xmlClassObject.GetType());
                StringWriter Output = new StringWriter(new StringBuilder());
                serializer.Serialize(Output, xmlClassObject);
                string tmpReturnString = Output.ToString().Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                return tmpReturnString;
            }
            catch
            {
                throw;
            }
        }

asp.net Get Xml Object

  public static T GetXmlObject<T>(string xmlString)
        {
            //Export to String
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                TextReader tr = new StringReader(xmlString);
                XmlReader xreader = new XmlTextReader(tr);
                T xmlClassObject = (T)serializer.Deserialize(xreader);
                return xmlClassObject;
            }
            catch
            {
                throw;
            }
        }

asp.net make first letter capital

  public static string CapitalFirstLetter(string StrText)
        {
            return new CultureInfo("en").TextInfo.ToTitleCase(StrText.ToLower());
        }

asp.net convert file to base64 string

public static string FileToBase64(string filePath)
        {
            byte[] data = null;

            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                long numBytes = new FileInfo(filePath).Length;
                //logger.LogInfo("Library.Functions.FileToBase64(): Path:" + filePath + " Bytes:" + numBytes);
                data = br.ReadBytes((int)numBytes);
                br.Close();
                fs.Close();
            }
            catch (BaseException ex)
            {
                logger.LogError("Library.Functions.FileToBase64(): Path:" + filePath + "; Exception: " + ex.ToString(), ex);
                return "";
            }

            if (data == null)
            {
                return "";
            }
            else
            {
                return Convert.ToBase64String(data);
            }
        }