Saturday, October 31, 2009

Code to Open pdf/doc/any file

This post is for opening any document in website
// function to open file
protected void OpenDocument()
{
// Get the physical Path of the file(test.doc)
string filepath = Server.MapPath("Document Location");

// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(filepath);

// Checking if file exists
if (file.Exists)
{
// Clear the content of the response
Response.ClearContent();

// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

// Add the file size into the response header
Response.AddHeader("Content-Length", file.Length.ToString());

// Set the ContentType
Response.ContentType = ReturnExtension(file.Extension.ToLower());

// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(file.FullName);

// End the response
Response.End();
}
}
// Function to get extenstion of the file
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}

Wednesday, October 28, 2009

MDI Parent Child

This post is for MDI Parent child relation in windows application.
the code detects whether child form is already opened or not.
If already opened then it did not allow to create a new child and reopens (maximise) the existed form.

here is the code for that
on click event of menu
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
// to check whether form is opened
if (!CheckExistingForm("VanMaster"))
{
VanMaster vanMaster = new VanMaster(this);
vanMaster.Show();
}
}
******************************
// method to check if propvided form is exists or open before
private bool CheckExistingForm(string formName)
{
int flag = 0;
Form[] forms = this.MdiChildren;
if (forms.Length > 0)
{
for (int i = 0; i < forms.Length; i++)
{
if (forms[i].Name == formName)
{
forms[i].WindowState = FormWindowState.Normal;
flag = 1;
break;
}
}
if (flag == 1)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

**********************************************************8
at VanMaster.cs file



public partial class VanMaster : Form
    {
        public VanMaster(Form Parent)
        {
            InitializeComponent();
            this.Parent = Parent;
        }
    }


Password Encryption Code

Code to encrypt a password
public string ecrypt(string pwd)
{
string temp = "";
int len = pwd.Length;
if (len == 5)
{
temp = pwd + pwd.Substring((len - 3), 3);
}
else if (len == 6)
{
temp = pwd + pwd.Substring((len - 2), 2);
}
else if (len == 7)
{
temp = pwd + pwd.Substring((len - 7), 1);
}
char[] chartemparr = new char[temp.Length];
chartemparr=temp.ToCharArray();
int[] inttemparr = new int[chartemparr.Length];
//chartemparr.CopyTo(inttemparr, 0);
int i=0,j=0;

for (i = 0; i < temp = "" i =" 0;" j =" inttemparr[i]+1;">);
}

return temp;
}