StesCodes

ASP CODES


Read Excel file without saving - ASP.net C#

  Stream filestream = file_upload.PostedFile.InputStream;
  StreamReader streamreader = new StreamReader(filestream);
  string excelcontent = streamreader.ReadToEnd();
       
   

To deserialize JSON - ASP.net C#

  using System.Web.Script.Serialization;
  public partial class aspcodes : System.Web.UI.Page
  {
     protected void Page_Load(object sender, EventArgs e)
     {
           JavaScriptSerializer serializer = new JavaScriptSerializer();
           serialdetails ys = serializer.Deserialize<serialdetails>
                                                  (strGetLoginPage_fb_y);
           Dictionary<string, object> dic = new Dictionary<string, 
                                                              object>();
           string teststr = "";
           for (int x = 0; x < Convert.ToInt32(ys.total_matches); x++)
            {
                dic = ((System.Collections.Generic.Dictionary<string, 
                             object>)(((object[])(ys.contacts))[x]));
                foreach (KeyValuePair<string, object> kvp in dic)
                {
                    teststr = kvp.Value.ToString();
                }
            }
      }
   }
   public class serialdetails
   {
        public string str1;
        public string str2;
        public object contacts;
   }
   

To generate random number between certain numbers - ASP.net C#

 Random rd = new Random();
 int getid = rd.Next(1000000);


To split string by large string - ASP.net C#

  using System.Text.RegularExpressions;
  
  string teststring = "sdfsdf~!@#jdfh~!@#iuhiuh~!@#iuhiuh~!@#iuhiuh~!@#";
  string[] myarray = Regex.Split(teststring,"~!@#");


To Generate random text - ASP.net C#

  string rtext = Guid.NewGuid().ToString();


To create a directory - ASP.net C#

   public void MakeDirectoryIfNotExists(string newdirectory)
   {
        if (!Directory.Exists(newdirectory))
          Directory.CreateDirectory(newdirectory);
   }


To delete all files and folders inside a directory but not the directory- ASP.net C#

 public void truncatedirectory(string path)
 {
      DirectoryInfo df = new DirectoryInfo(path);
      DirectoryInfo[] abc = df.GetDirectories();
      for (int i = 0; i < abc.Length; i++)
          abc[i].Delete(true);

      string[] abc_files = Directory.GetFiles(path);
      foreach (string a in abc_files)
      {
          File.Delete(a);
      } 
 
 }


Serialize to XML from string - ASP.net C#

 public void serializetoxml(string readtxt)
 {
     XmlDocument doc = new XmlDocument();
     doc.LoadXml(readtxt);
     XmlNode root = doc.DocumentElement;
     for(int i=0;i<root.ChildNodes.Count;i++)
     {
     ...
     }
 
 }

HOME | TERMS OF SERVICE | PRODUCT | CONTACT US | PRIVACY POLICY | FAQ | SITE MAP
Copyright 2011 stescodes.com