Tuesday 14 August 2012

App_code Folder issues


So I'm having a really weird issue with my App_Code folder on a new website I'm designing.
I have a basic class inside of a namespace in the App_Code folder. Everything works fine in the IDE when I setup the namespace and make an object from the class. It brings up the class summary on hover, and when you click on "go to deffinition" it goes to the class file. And it also works fine localy.

Ans:The problem that your classes are not compiled, You'll solve this issue simply by going to the properties of any class in the App_Code folder and change it's 'Build Action' property from "Content" to "Compile"

Friday 10 August 2012

Send Mail From Application in c#


MailMessage message = new MailMessage();
message.From = new MailAddress(From);
            foreach (string address in To)
                message.To.Add(address);
            if (null != bcc)
                foreach (string address in bcc)
                {
                    message.Bcc.Add(address);
                }

            message.Subject = Subject;
            message.Body = Body;
            message.IsBodyHtml = true;

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Host = "smptp.gmail.com";
            smtpClient.Port = 695;//port number
            smtpClient.EnableSsl = true;
            //if (false)
            //    smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
            //else
               smtpClient.Credentials = new NetworkCredential("admin@domain.com", "password");
            smtpClient.Send(message);
}

c# save contents of IE browser as html


If you embed a WebBrowser control inside of your WinForm you can do this:

webBrowser1.Navigate("http://google.com");

string pageHtml = webBrowser1.Document.GetElementsByTagName("html")[0].InnerHtml;


This will return all of the HTML in the page navigated to.


For Storing Html data in ASP>NET


protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
    {
        base.Render(htmlwriter);
        string html = htmlwriter.InnerWriter.ToString();


        using (FileStream outputStream = new FileStream(@"C:\\temp.html", FileMode.OpenOrCreate))
        {
             outputStream.Write(html, 0, html.Length);
             outputStream.Close();
        }

        writer.Write(html);
    }
}

Thursday 9 August 2012

File String Operations


//how to create a text file dynamically            
using System.IO;
string fileLoc = @"c:\sample1.txt";
            FileStream fs = null;
            if (!File.Exists(fileLoc))
            {
                using (fs = File.Create(fileLoc))
                {

                }
            }
(or)
string fileLoc = @"c:\sample1.txt";
FileStream fs= File.Create(fileLoc);
            

//writing to a text file
string fileLoc = @"c:\sample1.txt";
          
              
              
          if (File.Exists(fileLoc))//writing a file
          {
              using (StreamWriter sw = new StreamWriter(fileLoc))
             {
                  sw.Write("Some sample text for the file");
              }
          }

//To Open and read the content from file…
using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
String s=””;
S=sr.ReadLine();
}
}

//Read from file
if (File.Exists(fileLoc))
            {
                using (TextReader tr = new StreamReader(fileLoc))
                {
                    MessageBox.Show(tr.ReadLine());
                }
            }

//copy a file
string fileLocCopy = @"d:\sample1.txt";
            if (File.Exists(fileLoc))
            {
                // If file already exists in destination, delete it.
                if (File.Exists(fileLocCopy))
                    File.Delete(fileLocCopy);
                File.Copy(fileLoc, fileLocCopy);
            }


//Move a File
 string fileLocMove = @"d:\sample1" + System.DateTime.Now.Ticks + ".txt";
            if (File.Exists(fileLoc))
            {
                File.Move(fileLoc, fileLocMove);
            }

//Delete a file
if (File.Exists(fileLoc))
            {
                File.Delete(fileLoc);
            }


// Example #1: Write an array of strings to a file.
// Create a string array that consists of three lines.
            string[] lines = { "First line""Second line""Third line" };
            System.IO.File.WriteAllLines(fileLoc, lines);




// Example #3: Write only some strings in an array to a file.

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
            {
                foreach (string line in lines)
                {
                    if (line.Contains("Second") == false)
                    {
                        file.WriteLine(line);
                    }
                }
            }
// Example #4: Append new text to an existing file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
            {
                file.WriteLine("Fourth line");
            }  

Wednesday 8 August 2012

Getting the host name From url


Getting the host name From url

System.Uri url = new System.Uri(UserDomain);
                string host = url.Host;

Find Browser URL in C#

How to Find Browser URL in C# ?

A:
HTMLDocument htmlDoc = (HTMLDocument)this.Explorer.IWebBrowser_Document;