To change the row color of the GridView control on the onmouseover, onmouseout event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver'");
// This will be the back ground color of the GridView Control
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
}
}
tag:-To change the row color of the GridView control on the onmouseover event.
Articles
- Articles(C#.NET) (3)
- ASP.NET (6)
- Asp.Net(Email) (1)
- Asp.net(Image) (2)
- Asp.Net(Web.Config) (2)
- C#.NET (5)
- C#.NET Threading (3)
- C#.NET(ASP.NET) (4)
- Comments in PHP (1)
- Encryption In PHP (1)
- iPhone (Articles) (1)
- JavaScript (1)
- Json with PHP (1)
- LINQ (1)
- PHP (2)
- PHP Constant (1)
- PHP Operators (1)
- PHP Print Command (1)
- PHP Tutorial (2)
- Strings In PHP (1)
- Variable Declaration In PHP (1)
- WPF (1)
- XAML (2)
About Me
Help Link
Followers
RC4 Encryption in JavaScript
<html>
<head>
<title>RC4 Encryption</title>
</head>
<body>
<script language="JavaScript">
var dg=''
function makeArray(n) {
for (var i=1; i<=n; i ) {
this[i]=0
}
return this
}
function rc4(key, text) {
var i, x, y, t, x2;
status("rc4")
s=makeArray(0);
for (i=0; i<256; i ) {
s[i]=i
}
y=0
for (x=0; x<256; x ) {
y=(key.charCodeAt(x % key.length) s[x] y) % 256
t=s[x]; s[x]=s[y]; s[y]=t
}
x=0; y=0;
var z=""
for (x=0; x<text.length; x ) {
x2=x % 256
y=( s[x2] y) % 256
t=s[x2]; s[x2]=s[y]; s[y]=t
z = String.fromCharCode((text.charCodeAt(x) ^ s[(s[x2] s[y]) % 256]))
}
return z
}
function badd(a,b) { // binary add
var r=''
var c=0
while(a || b) {
c=chop(a) chop(b) c
a=a.slice(0,-1); b=b.slice(0,-1)
if(c & 1) {
r="1" r
} else {
r="0" r
}
c>>=1
}
if(c) {r="1" r}
return r
}
function chop(a) {
if(a.length) {
return parseInt(a.charAt(a.length-1))
} else {
return 0
}
}
function bsub(a,b) { // binary subtract
var r=''
var c=0
while(a) {
c=chop(a)-chop(b)-c
a=a.slice(0,-1); b=b.slice(0,-1)
if(c==0) {
r="0" r
}
if(c == 1) {
r="1" r
c=0
}
if(c == -1) {
r="1" r
c=1
}
if(c==-2) {
r="0" r
c=1
}
}
if(b || c) {return ''}
return bnorm(r)
}
function bnorm(r) { // trim off leading 0s
var i=r.indexOf('1')
if(i == -1) {
return '0'
} else {
return r.substr(i)
}
}
function bmul(a,b) { // binary multiply
var r=''; var p=''
while(a) {
if(chop(a) == '1') {
r=badd(r,b p)
}
a=a.slice(0,-1)
p ='0'
}
return r;
}
function bmod(a,m) { // binary modulo
return bdiv(a,m).mod
}
function bdiv(a,m) { // binary divide & modulo
// this.q = quotient this.mod=remainder
var lm=m.length, al=a.length
var p='',d
this.q=''
for(n=0; n<al; n ) {
p=p a.charAt(n);
if(p.length<lm || (d=bsub(p,m)) == '') {
this.q ='0'
} else {
if(this.q.charAt(0)=='0') {
this.q='1'
} else {
this.q ="1"
}
p=d
}
}
this.mod=bnorm(p)
return this
}
function bmodexp(x,y,m) { // binary modular exponentiation
var r='1'
status("bmodexp " x " " y " " m)
while(y) {
if(chop(y) == 1) {
r=bmod(bmul(r,x),m)
}
y=y.slice(0,y.length-1)
x=bmod(bmul(x,x),m)
}
return bnorm(r)
}
function modexp(x,y,m) { // modular exponentiation
// convert packed bits (text) into strings of 0s and 1s
return b2t(bmodexp(t2b(x),t2b(y),t2b(m)))
}
function i2b(i) { // convert integer to binary
var r=''
while(i) {
if(i & 1) { r="1" r} else {r="0" r}
i>>=1;
}
return r? r:'0'
}
function t2b(s) {
var r=''
if(s=='') {return '0'}
while(s.length) {
var i=s.charCodeAt(0)
s=s.substr(1)
for(n=0; n<8; n ) {
r=((i & 1)? '1':'0') r
i>>=1;
}
}
return bnorm(r)
}
function b2t(b) {
var r=''; var v=0; var m=1
while(b.length) {
v|=chop(b)*m
b=b.slice(0,-1)
m<<=1
if(m==256 || b=='') {
r =String.fromCharCode(v)
v=0; m=1
}
}
return r
}
b64s='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"'
function textToBase64(t) {
status("t 2 b64")
var r=''; var m=0; var a=0; var tl=t.length-1; var c
for(n=0; n<=tl; n ) {
c=t.charCodeAt(n)
r =b64s.charAt((c << m | a) & 63)
a = c >> (6-m)
m =2
if(m==6 || n==tl) {
r =b64s.charAt(a)
if((n%45)==44) {r ="\n"}
m=0
a=0
}
}
return r
}
function base64ToText(t) {
status("b64 2 t")
var r=''; var m=0; var a=0; var c
for(n=0; n<t.length; n ) {
c=b64s.indexOf(t.charAt(n))
if(c >= 0) {
if(m) {
r =String.fromCharCode((c << (8-m))&255 | a)
}
a = c >> m
m =2
if(m==8) { m=0 }
}
}
return r
}
function rand(n) { return Math.floor(Math.random() * n) }
function rstring(s,l) {
var r=""
var sl=s.length
while(l-->0) {
r =s.charAt(rand(sl))
}
//status("rstring " r)
return r
}
function key2(k) {
var l=k.length
var kl=l
var r=''
while(l--) {
r =k.charAt((l*3)%kl)
}
return r
}
function rsaEncrypt(keylen,key,mod,text) {
// I read that rc4 with keys larger than 256 bytes doesn't significantly
// increase the level of rc4 encryption because it's sbuffer is 256 bytes
// makes sense to me, but what do i know...
status("encrypt")
if(text.length >= keylen) {
var sessionkey=rc4(rstring(text,keylen),rstring(text,keylen))
// session key must be less than mod, so mod it
sessionkey=b2t(bmod(t2b(sessionkey),t2b(mod)))
alert("sessionkey=" sessionkey)
// return the rsa encoded key and the encrypted text
// i'm double encrypting because it would seem to me to
// lessen known-plaintext attacks, but what do i know
return modexp(sessionkey,key,mod)
rc4(key2(sessionkey),rc4(sessionkey,text))
} else {
// don't need a session key
return modexp(text,key,mod)
}
}
function rsaDecrypt(keylen,key,mod,text) {
status("decrypt")
if(text.length <= keylen) {
return modexp(text,key,mod)
} else {
// sessionkey is first keylen bytes
var sessionkey=text.substr(0,keylen)
text=text.substr(keylen)
// un-rsa the session key
sessionkey=modexp(sessionkey,key,mod)
alert("sessionkey=" sessionkey)
// double decrypt the text
return rc4(sessionkey,rc4(key2(sessionkey,text),text))
}
}
function trim2(d) { return d.substr(0,d.lastIndexOf('1') 1) }
function bgcd(u,v) { // return greatest common divisor
// algorythm from http://algo.inria.fr/banderier/Seminar/Vallee/index.html
var d, t
while(1) {
d=bsub(v,u)
//alert(v " - " u " = " d)
if(d=='0') {return u}
if(d) {
if(d.substr(-1)=='0') {
v=d.substr(0,d.lastIndexOf('1') 1) // v=(v-u)/2^val2(v-u)
} else v=d
} else {
t=v; v=u; u=t // swap u and v
}
}
}
function isPrime(p) {
var n,p1,p12,t
p1=bsub(p,'1')
t=p1.length-p1.lastIndexOf('1')
p12=trim2(p1)
for(n=0; n<2; n =mrtest(p,p1,p12,t)) {
if(n<0) return 0
}
return 1
}
function mrtest(p,p1,p12,t) {
// Miller-Rabin test from forum.swathmore.edu/dr.math/
var n,a,u
a='1' rstring('01',Math.floor(p.length/2)) // random a
//alert("mrtest " p ", " p1 ", " a "-" p12)
u=bmodexp(a,p12,p)
if(u=='1') {return 1}
for(n=0;n<t;n ) {
u=bmod(bmul(u,u),p)
//dg =u " "
if(u=='1') return -100
if(u==p1) return 1
}
return -100
}
pfactors='11100011001110101111000110001101'
// this number is 3*5*7*11*13*17*19*23*29*31*37
function prime(bits) {
// return a prime number of bits length
var p='1' rstring('001',bits-2) '1'
while( ! isPrime(p)) {
p=badd(p,'10'); // add 2
}
alert("p is " p)
return p
}
function genkey(bits) {
q=prime(bits)
do {
p=q
q=prime(bits)
} while(bgcd(p,q)!='1')
p1q1=bmul(bsub(p,'1'),bsub(q,'1'))
// now we need a d, e, and an n so that:
// p1q1*n-1=de -> bmod(bsub(bmul(d,e),'1'),p1q1)='0'
// or more specifically an n so that d & p1q1 are rel prime and factor e
n='1' rstring('001',Math.floor(bits/3) 2)
alert('n is ' n)
factorMe=badd(bmul(p1q1,n),'1')
alert('factor is ' factorMe)
//e=bgcd(factorMe,p1q1)
//alert('bgcd=' e)
e='1'
// is this always 1?
//r=bdiv(factorMe,e)
//alert('r=' r.q " " r.mod)
//if(r.mod != '0') {alert('Mod Error!')}
//factorMe=r.q
d=bgcd(factorMe,'11100011001110101111000110001101')
alert('d=' d)
if(d == '1' && e == '1') {alert('Factoring failed ' factorMe ' p=' p ' q=' q)}
e=bmul(e,d)
r=bdiv(factorMe,d)
d=r.q
if(r.mod != '0') {alert('Mod Error 2!')}
this.mod=b2t(bmul(p,q))
this.pub=b2t(e)
this.priv=b2t(d)
}
function status(a) { }//alert(a)}
</script>
<script language="JavaScript">
<!--
function rc4encrypt() {
document.rc4.text.value=textToBase64(rc4(document.rc4.key.value,document.rc4.text.value))
}
function rc4decrypt() {
document.rc4.text.value=(rc4(document.rc4.key.value,base64ToText(document.rc4.text.value)))
}
-->
</script>
<h3>RC4 Encryption (input text for both field):</h3>
<form method="POST" name="rc4">
<div align="center">
<table border="0">
<tr>
<td align="right">
Key:
</td>
<td>
<input type="text" size="60" name="key" value="">
</td>
</tr>
<tr>
<td align="right">
Text:
</td>
<td>
<textarea name="text" rows="6" cols="70"></textarea>
</td>
</tr>
<tr>
<td>
<p align="center">
<input type="button" name="B1" value="Encrypt" onclick="rc4encrypt()">
<input type="button" name="B2" value="Decrypt" onclick="rc4decrypt()">
</p>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
To compute the GridView Sum in asp.net let’s take a simple example of SQL Database table. For GridView sum in ASP.net you can use the Northwind SQL database table to test the source code given in this tutorial. Here we will use products SQL table of Northwind database. In this tutorial we will compute the sum of units in stock of each product in the specified category.
Now you are ready to connect this SQL table with ASP.net web page to display the records in SQL table. But still you need a control to display the records in tabular format so that you could show the sum of column exactly below the Units in Stock column of GridView control.
HTML code with Auto Formatted GridView in ASP.Net Web Page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="2" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Product Name" HeaderStyle-Width="200px">
<ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "ProductName") %>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label1" runat="server" Text="Total Units">
</asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Units In Stock">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "UnitsInStock") %></ItemTemplate>
<FooterTemplate><asp:Label ID="Label2" runat="server"></asp:Label></FooterTemplate></asp:TemplateField></Columns><HeaderStyle HorizontalAlign="Left" /><FooterStyle BackColor="#cccccc" Font-Bold="true" /></asp:GridView>
In the above HTML code of GridView control of ASP.Net 2.0 you can notice that we have created two columns of GridView using ItemTemplate of TemplateField to display the Product Name and Units in stock Columns of Products Table. In both TemplateField columns we also added FooterTemplate sections having Label controls. Under product name column Text property of Label control has been set to "Total Unit". For the Label control placed inside the FooterTemplate of second TemplateField displaying the Units In Stock column we will set its Text property dynamically after computing the sum of associated column.
C# Sample code to Compute the Sum of Column in GridView control of ASP.Net
SqlCommand mySqlCommand = new SqlCommand("select productid, productname, unitsinstock from products where categoryid = @categoryid", mySQLconnection);
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
mySqlCommand.Parameters.Add("@categoryid", SqlDbType.Int).Value = 1;
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;GridView1.DataBind();((Label)GridView1.FooterRow.Cells[1].FindControl("Label2")).Text = myDataSet.Tables[0].Compute("sum(unitsinstock)", "").ToString();
In the above C# code Compute function is used that returns the passed expression for all the rows of a table in a dataset. According to above code, it will return the sum of units in stock for each product in the specified category.
Compute function of DataTable in ASP.Net
Compute function takes 2 parameters:
1.String Expression:
Expression as a agregate function of sql.
2.String Filter:
Filter criteria to filter the retrived rows.
E.g.:
((Label)GridView1.FooterRow.Cells[1].FindControl("Label2")).Text = myDataSet.Tables[0].Compute("sum(unitsinstock)", "categoryid=1").ToString();
Above C# example code line shows that you can also specify the string Filter as a second parameter of Compute function to evaluate the result according to to specified string expression as aggregate function as a first parameter.
tag:-ASP.Net 2.0 GridView Compute Column Sum using C#
This tutorial will show you how to display data using the .NET GridView Control, stored procedures, ASP.NET 2.0 and C#.NET
Querying an SQL database with stored procedures using C# .NET is easy to do.
First, you will need to import the System.Data.SqlClient namespace.
The System.Data.SqlClient namespace contains the SqlCommand and SqlConnection classes that we need in order to connect to our database and to send an SQL command to it.
using System.Data.SqlClient;
We'll put our code in the Page_Load() event.
When the Page_Load() event fires, a new SqlCommand object is instantiated with our connection string and our stored procedure name. We add the parameters needed for the stored procedure by using our SqlCommand object's Parameters.Add() method.
Afterwards, we will attempt to connect using the Open() method of our cmd.Connection object. Once it is connected we will attempt to execute the stored procedure we specified earlier (in this example CustOrderHist stored procedure in the Northwind db).
If all goes well, we will have the results of our SQL query assigned to the gvwExample's DataSource property. Now all we have to do is call the DataBind() method of our gvwExample to bind the data to the control. The data is now ready to be displayed.
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("CustOrderHist", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("CustomerID", txtCustID.Text);
cmd.Connection.Open();
gvwExample.DataSource = cmd.ExecuteReader();
gvwExample.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}
We have to add a few tags on the front end of the .aspx page to place where we want the GridView control to display its bound data. We also specify what part of the data from the data set we would like to display. The front end .aspx page looks something like this:
<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1"> Customer Data Using Stored Procedures:</td>
<td align="center" bgcolor="#FFFFFF">
<asp:GridView ID="gvwExample" runat="server" AutoGenerateColumns="False" CssClass="basix" >
<columns>
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField DataField="Total" HeaderText="Total" />
</columns>
</asp:GridView>
<br />
Customer ID:
<asp:TextBox ID="txtCustID" runat="server" Width="42px">ALFKI</asp:TextBox>
Order ID:<asp:TextBox ID="txtOrderID" runat="server" Width="43px">10256</asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" Text="Go" />
<br />
<asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>
The flow for the code behind page is as follows.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("CustOrderHist", new SqlConnection("Server=localhost;Database=Northwind;Trusted_Connection=True;"));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("CustomerID", txtCustID.Text);
cmd.Connection.Open();
gvwExample.DataSource = cmd.ExecuteReader();
gvwExample.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
}
}
}
tag:- Using Stored Procs with ASP.NET 2.0 GridView and C#
Related Articles
Find Checkbox control in the Gridview and Check box is checked or not
Get Cell Contents in GridView using C#.Net
How To Find HeaderRow in GridView Using C#.Net in Asp.net
How to populate Treeview Control in Asp.Net using C# . Add Treeview Control from the toolbox .
Id : TreeView1
Sample application for Treeview Control
protected void Button1_Click(object sender, EventArgs e)
{
TreeNode tNode = new TreeNode();
tNode.Text = "Sreejith";
TreeView1.Nodes.Add(tNode);
TreeView1.Nodes[0].Checked = true;
for (int i = 0; i < 5; i++)
{
TreeNode tNode1 = new TreeNode();
tNode1.Text = i.ToString();
TreeView1.Nodes[0].ChildNodes.Add(tNode1);
if (i % 2 == 0)
{
TreeView1.Nodes[0].ChildNodes[i].Checked = true;
}
for (int j = 0; j < 2; j++)
{
TreeNode tNode2 = new TreeNode();
tNode2.Text = j.ToString();
TreeView1.Nodes[0].ChildNodes[i].ChildNodes.Add(tNode2);
}
}
}
tage:- How to populate Treeview Control in Asp.Net using C# . Add Treeview Control from the toolbox
Find Checkbox control in each row of the Gridview and Check box is checked or not and also select and deselect all checkbox in the Gridview .Implement Findcontrol method in the Gridview ..
/// Find check box control in the Gridview and Check box is checked or not
protected void btnList_Click(object sender, EventArgs e)
{
//Get number of rows in the GridView
int iCount = this.grdCourse.Rows.Count;
this.lstCourseId.Items.Clear();
for (int iIndex = 0; iIndex < iCount; iIndex++)
{
//Find Checkbox control in each row of the gridview..
CheckBox chkCourceId = ((CheckBox)this.grdCourse.Rows[iIndex].FindControl("chkId"));
//If checkbox is checked
if (chkCourceId.Checked)
{
//Get Course Id in each row of the gridview..
string strCourceId = this.grdCourse.Rows[iIndex].Cells[1].Text.ToString();
//Add CourseId Value into the Listbox Control
this.lstCourseId.Items.Add(strCourceId);
}
}
}
// Select and deselect all checkboxes in te Griview Control
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
//Get number of rows in the GridView
int iCount = this.grdCourse.Rows.Count;
//If Select All check Box is checked
if (this.chkSelectAll.Checked)
{
for (int iIndex = 0; iIndex < iCount; iIndex++)
{
//Find Checkbox control in each row of the gridview..
CheckBox chkCourceId = ((CheckBox)this.grdCourse.Rows[iIndex].FindControl("chkId"));
// If checkbox is not checked
if (!chkCourceId.Checked)
{
chkCourceId.Checked = true;
}
}
}
else
{
for (int iIndex = 0; iIndex < iCount; iIndex++)
{
//Find Checkbox control in each row of the gridview..
CheckBox chkCourceId = ((CheckBox)this.grdCourse.Rows[iIndex].FindControl("chkId"));
// If checkbox is checked in the gridview
if (chkCourceId.Checked)
{
chkCourceId.Checked = false;
}
}
}
}
Please aply these code into the design (html) page...
<asp:GridView ID="grdCourse" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkID" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnList" runat="server" OnClick="btnList_Click" Text="List" />
<asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="True"
OnCheckedChanged="chkSelectAll_CheckedChanged" Text="Select All" />
tag:-
Dowload a YouTube Video Usong C#.Net .How can download YouTube Video using C#.net in windows application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Net;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
namespace DownloadYouTube
{
public partial class Form1 : Form
{
string current_working_directory = Application.StartupPath; // This is used to determine where to save the video.
string youtube_video_title = "video"; // This gets set near the end of get_download_url()
string strUrl = @"http://www.youtube.com/watch?v=_QUrWjEGj34&feature=fvhl";///* insert video URL here*/
public Form1()
{
InitializeComponent();
}
private void btnDownload_Click(object sender, EventArgs e)
{
string youtube_url = makeYouTubeURLCompliant(strUrl); // This makes the URL compliant with the code.
string download_url = get_download_url(youtube_url); // This extracts the download URL from the YouTube website HTMl source code.
download_video_from_url(download_url); // This actually downloads the video. I haven't tested this as I used someone elses queue download class for my own downloading needs.
}
public string get_download_url(string url)
{
string download_url = null;
string buffer = getYouTubePageContent(url);
string title = string.Empty;
if (buffer.IndexOf("Error:") < 0)
{
int start = 0, end = 0;
string startTag = "/watch_fullscreen?";
string endTag = ";";
start = buffer.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase);
end = buffer.IndexOf(endTag, start, StringComparison.CurrentCultureIgnoreCase);
string str = buffer.Substring(start + startTag.Length, end - (start + startTag.Length));
string vid = str.Substring(str.IndexOf("video_id"), str.IndexOf("&", str.IndexOf("video_id")) - str.IndexOf("video_id"));
string l = str.Substring(str.IndexOf("&l"), str.IndexOf("&", str.IndexOf("&l") + 1) - str.IndexOf("&l"));
string t = str.Substring(str.IndexOf("&t"), str.IndexOf("&", str.IndexOf("&t") + 1) - str.IndexOf("&t"));
title = str.Substring(str.IndexOf("&title=") + 7);
download_url = "http://youtube.com/get_video?" + vid + l + t;
}
else
{
MessageBox.Show("Error downloading video");
}
youtube_video_title = title;
return download_url;
}
private string makeYouTubeURLCompliant(string url)
{
url = url.Replace("www.youtube.com", "youtube.com");
if (url.IndexOf("http://youtube.com/v/") >= 0)
{
url.Replace("http://youtube.com/v/", "http://youtube.com/watch?v=");
}
if (url.IndexOf("http://youtube.com/watch?v=") < 0)
{
url = "";
}
return (url);
}
// This is a helper method is used by get_download_url()
private string getYouTubePageContent(string url)
{
string buffer;
try
{
string outputBuffer = "where=46038";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentLength = outputBuffer.Length;
req.ContentType = "application/x-www-form-urlencoded";
StreamWriter swOut = new StreamWriter(req.GetRequestStream());
swOut.Write(outputBuffer);
swOut.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
buffer = sr.ReadToEnd();
sr.Close();
}
catch (Exception exp)
{
buffer = "Error: " + exp.Message.ToString();
}
return (buffer);
}
private void download_video_from_url(string url)
{
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri(url), @current_working_directory + "\\" + makeTitleFileCompliant(youtube_video_title) + ".flv");
}
// This method makes the YouTube title compliant as a filename
public string makeTitleFileCompliant(string title_uncomp)
{
return title_uncomp.Replace("\\", "").Replace("/", "").Replace("*", "").Replace(":", "").Replace("?", "").Replace("\"", "").Replace("<", "").Replace(">", "").Replace("|", "");
}
}
}