2008년 10월 29일 수요일

Javascript event function

팝업창의 F11 기능 막기


document.onkeydown = function() {

event.keyCode = 0; // 핵심!

return false;

}

HTML Parsing in .Net


Html Agility Pack


HtmlDocument doc = new HtmlDocument();
HtmlAttribute attr = null;
String uri = "";

doc.Load(file, Encoding.UTF8);
if (doc.DocumentNode.SelectNodes("//img[@src]") != null)
{
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//img[@src]"))
{
uri = node.GetAttributeValue("src", "");
attr = node.SetAttributeValue("src", String.Format("{0}{1}", virtualPath, uri));
}
}
if (doc.DocumentNode.SelectNodes("//td[@background]") != null)
{
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//td[@background]"))
{
uri = node.GetAttributeValue("background", "");
attr = node.SetAttributeValue("background", String.Format("{0}{1}", virtualPath, uri));
}
}
doc.Save(file + ".htm", Encoding.UTF8);

2008년 10월 22일 수요일

[C#] SharpZipLib을 사용한 복수 개의 파일 압축 및 해제

// 압축
void Compression()
{
try
{
string zipPath = "test.zip";
System.IO.FileStream writer = new System.IO.FileStream( zipPath,
System.IO.FileMode.Create,
System.IO.FileAccess.Write, System.IO.FileShare.Write);

ICSharpCode.SharpZipLib.Zip.ZipOutputStream zos =
new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(writer);

foreach (string file in DiffFiles)
{
int Substringindex = textBox2.Text.Length;
string f = file.Substring(Substringindex + 1);

ICSharpCode.SharpZipLib.Zip.ZipEntry ze =
new ICSharpCode.SharpZipLib.Zip.ZipEntry(f);

System.IO.FileStream fs = new System.IO.FileStream( file,
System.IO.FileMode.Open, System.IO.FileAccess.Read,
System.IO.FileShare.Read);

byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();

ze.Size = buffer.Length;

ze.DateTime = DateTime.Now;

// 새로운 엔트리(파일)을 넣는다.
zos.PutNextEntry(ze);

// 쓰기
zos.Write(buffer, 0, buffer.Length);
}

zos.Close();
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

// 해제
void DeCompression(string filename)
{
string zipPath = filename;
string extractDir = Environment.CurrentDirectory;

System.IO.FileStream fs = new System.IO.FileStream( zipPath,
System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);

ICSharpCode.SharpZipLib.Zip.ZipInputStream zis =
new ICSharpCode.SharpZipLib.Zip.ZipInputStream(fs);

ICSharpCode.SharpZipLib.Zip.ZipEntry ze;

while ((ze = zis.GetNextEntry()) != null)
{
if (!ze.IsDirectory)
{
string fileName = System.IO.Path.GetFileName(ze.Name);

string destDir = System.IO.Path.Combine(extractDir,
System.IO.Path.GetDirectoryName(ze.Name));

if (false == Directory.Exists(destDir))
{
System.IO.Directory.CreateDirectory(destDir);
}

string destPath = System.IO.Path.Combine(destDir, fileName);

System.IO.FileStream writer = new System.IO.FileStream(
destPath, System.IO.FileMode.Create,
System.IO.FileAccess.Write,
System.IO.FileShare.Write);

byte[] buffer = new byte[2048];
int len;
while ((len = zis.Read(buffer, 0, buffer.Length)) > 0)
{
writer.Write(buffer, 0, len);
}

writer.Close();
}
}

zis.Close();
fs.Close();
}

포스트 백 후에도 같은 스크롤위치 고정시키기

지랄스럽게도 포스트백이 되버리면 페이지가 리로드 되는 것처럼 스크롤 포지션을 잃어버린다.asp.net2.0 에서는 무지하게 쉽게 스크롤 위치를 유지해준다.

MaintainScrollPositionOnPostback="true"

2008년 10월 21일 화요일

C#.Net 폴더 삭제

private void DeleteFolder(string dirPath, bool bRecursive)

{

DirectoryInfo dir = new DirectoryInfo(dirPath);

if (bRecursive)

{

DirectoryInfo[] directories = dir.GetDirectories();

foreach (DirectoryInfo di in directories)

{

DeleteFolder(di.FullName, bRecursive);

}

}

FileInfo[] files = dir.GetFiles();

foreach (FileInfo fi in files)

{

fi.Delete();

}

}