Thursday, April 16, 2009
Delete all temparary file of Internet explorer
using System.IO;
public static void Main()
#
{
ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))); // Execute ClearFolder() on the IE's cache folder
}
void ClearFolder(DirectoryInfo diPath)
{
foreach (FileInfo fiCurrFile in diPath.GetFiles())
{
fiCurrFile.Delete();
}
foreach (DirectoryInfo diSubFolder in diPath.GetDirectories())
{
ClearFolder(diSubFolder); // Call recursively for all subfolders
}
public static void Main()
#
{
ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))); // Execute ClearFolder() on the IE's cache folder
}
void ClearFolder(DirectoryInfo diPath)
{
foreach (FileInfo fiCurrFile in diPath.GetFiles())
{
fiCurrFile.Delete();
}
foreach (DirectoryInfo diSubFolder in diPath.GetDirectories())
{
ClearFolder(diSubFolder); // Call recursively for all subfolders
}
Thursday, March 12, 2009
Access text box value from masterpage
// put this code in content page,the text box control is in masterpage
TextBox txt = (TextBox)Master.FindControl("txtSearch");
string str = txt.Text;
TextBox txt = (TextBox)Master.FindControl("txtSearch");
string str = txt.Text;
Wednesday, February 4, 2009
Image Uploading
if (fluAddImage.FileName != "")
{
FileInfo Finfo = new FileInfo(fluAddImage.FileName);
string Ext = Finfo.Extension.ToLower();
if (Ext == ".jpg" || Ext == ".jpeg" || Ext == ".gif" || Ext == ".bmp" || Ext == ".png")
{
string rootfolder = GetPhysicalPath(Application["HomePath"] + Global.MemberPhotoPath);//path e.g http://localhost:4578/project/Imagesg
if (!Directory.Exists(rootfolder))
{
Directory.CreateDirectory(rootfolder);
}
DirectoryInfo dirInfo = new DirectoryInfo(rootfolder);
string PhotoName ="xyz" + Ext;
string Filename = rootfolder + PhotoName;
fluAddImage.SaveAs(Filename);
}
}
public static string GetPhysicalPath(string virtualPath)
{
string pageUrl = HttpContext.Current.Request.Url.PathAndQuery.ToString();
string filename = "";
filename = System.IO.Path.GetFileName(virtualPath);
string filepath = "";
if (virtualPath.IndexOf("localhost:") >= 0)
{
string path = virtualPath.Replace(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host, "");
if (path.IndexOf(":") >= 0)
{
int lenindex = path.IndexOf('/');
path = path.Remove(0, lenindex);
}
filepath = HttpContext.Current.Server.MapPath(path.Replace(HttpContext.Current.Request.Url.Scheme + "://www." + HttpContext.Current.Request.Url.Host, ""));
}
else
{
string path = "";
path = virtualPath.Replace(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host, "");
filepath = HttpContext.Current.Server.MapPath(path.Replace(HttpContext.Current.Request.Url.Scheme + "://www." + HttpContext.Current.Request.Url.Host, ""));
}
return filepath;
}
{
FileInfo Finfo = new FileInfo(fluAddImage.FileName);
string Ext = Finfo.Extension.ToLower();
if (Ext == ".jpg" || Ext == ".jpeg" || Ext == ".gif" || Ext == ".bmp" || Ext == ".png")
{
string rootfolder = GetPhysicalPath(Application["HomePath"] + Global.MemberPhotoPath);//path e.g http://localhost:4578/project/Imagesg
if (!Directory.Exists(rootfolder))
{
Directory.CreateDirectory(rootfolder);
}
DirectoryInfo dirInfo = new DirectoryInfo(rootfolder);
string PhotoName ="xyz" + Ext;
string Filename = rootfolder + PhotoName;
fluAddImage.SaveAs(Filename);
}
}
public static string GetPhysicalPath(string virtualPath)
{
string pageUrl = HttpContext.Current.Request.Url.PathAndQuery.ToString();
string filename = "";
filename = System.IO.Path.GetFileName(virtualPath);
string filepath = "";
if (virtualPath.IndexOf("localhost:") >= 0)
{
string path = virtualPath.Replace(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host, "");
if (path.IndexOf(":") >= 0)
{
int lenindex = path.IndexOf('/');
path = path.Remove(0, lenindex);
}
filepath = HttpContext.Current.Server.MapPath(path.Replace(HttpContext.Current.Request.Url.Scheme + "://www." + HttpContext.Current.Request.Url.Host, ""));
}
else
{
string path = "";
path = virtualPath.Replace(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host, "");
filepath = HttpContext.Current.Server.MapPath(path.Replace(HttpContext.Current.Request.Url.Scheme + "://www." + HttpContext.Current.Request.Url.Host, ""));
}
return filepath;
}
Wednesday, January 7, 2009
Calculate file size of Uploading file
HttpPostedFile MyFile;
int FileLen;
System.IO.Stream MyStream;
System.Web.HttpFileCollection MyFileCollection = Request.Files;
MyFile = MyFileCollection[0];
FileLen = MyFile.ContentLength;
int FileLen;
System.IO.Stream MyStream;
System.Web.HttpFileCollection MyFileCollection = Request.Files;
MyFile = MyFileCollection[0];
FileLen = MyFile.ContentLength;
Wednesday, October 29, 2008
Export CSV to Database
public DataTable ParseCSV(string inputString)
{
DataTable dt = new DataTable();
// declare the Regular Expression that will match versus the input string
Regex re = new Regex("((?[^\",\\r\\n]+)|\"(?([^\"]|\"\")+)\")(,|(?\\r\\n|\\n|$))");
ArrayList colArray = new ArrayList();
ArrayList rowArray = new ArrayList();
int colCount = 0;
int maxColCount = 0;
string rowbreak = "";
string field = "";
MatchCollection mc = re.Matches(inputString);
foreach (Match m in mc)
{
// retrieve the field and replace two double-quotes with a single double-quote
field = m.Result("${field}").Replace("\"\"", "\"");
rowbreak = m.Result("${rowbreak}");
if (field.Length > 0)
{
colArray.Add(field);
colCount++;
}
if (rowbreak.Length > 0)
{
// add the column array to the row Array List
rowArray.Add(colArray.ToArray());
// create a new Array List to hold the field values
colArray = new ArrayList();
if (colCount > maxColCount)
maxColCount = colCount;
colCount = 0;
}
}
if (rowbreak.Length == 0)
{
// this is executed when the last line doesn't
// end with a line break
rowArray.Add(colArray.ToArray());
if (colCount > maxColCount)
maxColCount = colCount;
}
// create the columns for the table
for (int i = 0; i < maxColCount; i++)
dt.Columns.Add(String.Format("col{0:000}", i));
// convert the row Array List into an Array object for easier access
Array ra = rowArray.ToArray();
for (int i = 0; i < ra.Length; i++)
{
// create a new DataRow
DataRow dr = dt.NewRow();
// convert the column Array List into an Array object for easier access
Array ca = (Array)(ra.GetValue(i));
// add each field into the new DataRow
for (int j = 0; j < ca.Length; j++)
dr[j] = ca.GetValue(j);
// add the new DataRow to the DataTable
dt.Rows.Add(dr);
}
// in case no data was parsed, create a single column
if (dt.Columns.Count == 0)
dt.Columns.Add("NoData");
return dt;
}
public DataTable ParseCSVFile(string path)
{
string inputString = "";
// check that the file exists before opening it
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
inputString = sr.ReadToEnd();
sr.Close();
}
return ParseCSV(inputString);
}
{
DataTable dt = new DataTable();
// declare the Regular Expression that will match versus the input string
Regex re = new Regex("((?
ArrayList colArray = new ArrayList();
ArrayList rowArray = new ArrayList();
int colCount = 0;
int maxColCount = 0;
string rowbreak = "";
string field = "";
MatchCollection mc = re.Matches(inputString);
foreach (Match m in mc)
{
// retrieve the field and replace two double-quotes with a single double-quote
field = m.Result("${field}").Replace("\"\"", "\"");
rowbreak = m.Result("${rowbreak}");
if (field.Length > 0)
{
colArray.Add(field);
colCount++;
}
if (rowbreak.Length > 0)
{
// add the column array to the row Array List
rowArray.Add(colArray.ToArray());
// create a new Array List to hold the field values
colArray = new ArrayList();
if (colCount > maxColCount)
maxColCount = colCount;
colCount = 0;
}
}
if (rowbreak.Length == 0)
{
// this is executed when the last line doesn't
// end with a line break
rowArray.Add(colArray.ToArray());
if (colCount > maxColCount)
maxColCount = colCount;
}
// create the columns for the table
for (int i = 0; i < maxColCount; i++)
dt.Columns.Add(String.Format("col{0:000}", i));
// convert the row Array List into an Array object for easier access
Array ra = rowArray.ToArray();
for (int i = 0; i < ra.Length; i++)
{
// create a new DataRow
DataRow dr = dt.NewRow();
// convert the column Array List into an Array object for easier access
Array ca = (Array)(ra.GetValue(i));
// add each field into the new DataRow
for (int j = 0; j < ca.Length; j++)
dr[j] = ca.GetValue(j);
// add the new DataRow to the DataTable
dt.Rows.Add(dr);
}
// in case no data was parsed, create a single column
if (dt.Columns.Count == 0)
dt.Columns.Add("NoData");
return dt;
}
public DataTable ParseCSVFile(string path)
{
string inputString = "";
// check that the file exists before opening it
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
inputString = sr.ReadToEnd();
sr.Close();
}
return ParseCSV(inputString);
}
Thursday, October 16, 2008
Subscribe to:
Posts (Atom)