반응형
·디렉터리 및 파일 제어
§디렉터리와 파일은 각각 DirectoryInfo 클래스와 FileInfo 클래스를 사용하여 제어할 수 있습니다.
§[예제 8-3] 디렉터리 및 파일 제어 예제
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)// 유효성 성공여부를 나타내는 값을 가져오는것
{
string strDir = @"D:\DATABANK\";
string strId = TextBox1.Text; // 아이디값~!!!!!
strDir += strId + @"\"; //아이디와 역슬러시를 붙여줘야된다 디렉토리명완성
DirectoryInfo di = new DirectoryInfo(strDir); //디렉토리 정보
if (!di.Exists) // 디렉토리가 있는지 여부를 가져오는 값불이다 ㅎㅎ
{
di.Create();
}
string originfile = FileUpload1.PostedFile.FileName;//컨트롤의 업로드파일내부
string destFile = strDir + FileUpload1.FileName; // 업로드할 파일네임
FileStream fileOpen = new FileStream(originfile, FileMode.Open);// 읽기위한파일스트림
FileStream fd = new FileStream(destFile, FileMode.Create); //쓰기위한 파일스트림
// Append 는 출력파일을 만드는데 출력 대상 파일이 존대하면 존재하는 파일의 끝에 새로운 내용을 추가
int i = 0;
do
{
i = fileOpen.ReadByte();
if (i != -1)
{
fd.WriteByte((byte)i);
}
} while (i != -1);
fileOpen.Close();
fd.Close();
Label1.Text = "원본파일 : " + originfile;
Label2.Text = "복사파일 : " + destFile;
}
////////////////////////////////////디렉토리만들면서 생성하기 ////////////////////////
//////////////////////////////////// 중복파일 못하게 만들기 ////////////////////////
protected void Button1_Click(object sender, EventArgs e)
{
if (Page.IsValid)// 유효성 성공여부를 나타내는 값을 가져오는것
{
string strDir = @"D:\DATABANK\";
string strId = TextBox1.Text; // 아이디값~!!!!!
strDir += strId + @"\"; //아이디와 역슬러시를 붙여줘야된다 디렉토리명완성
DirectoryInfo di = new DirectoryInfo(strDir); //디렉토리 정보
if (!di.Exists) // 디렉토리가 있는지 여부를 가져오는 값불이다 ㅎㅎ
{
di.Create();
}
string originfile = FileUpload1.PostedFile.FileName;//컨트롤의 업로드파일내부
string destFile = strDir + FileUpload1.FileName; // 업로드할 파일네임
//FileStream fileOpen = new FileStream(originfile, FileMode.Open);// 읽기위한파일스트림
//FileStream fd = new FileStream(destFile, FileMode.Create); //쓰기위한 파일스트림
// Append 는 출력파일을 만드는데 출력 대상 파일이 존대하면 존재하는 파일의 끝에 새로운 내용을 추가
FileInfo fi = new FileInfo(destFile);
if (fi.Exists) // 같은 파일이 존재한다면
{
int fIndex = 0;
string fExtension = fi.Extension;// 파일이 확장부분을 가져옴
string fRealName = FileUpload1.FileName.Replace(fExtension, ""); //확장자빼고 나머지이름
string fNewName = string.Empty;
do
{
fIndex++;
fNewName= fRealName+"_"+fIndex.ToString()+ fExtension;
fi = new FileInfo(strDir + fNewName);
} while (fi.Exists);
{
destFile = strDir + fNewName;
}
}
FileUpload1.PostedFile.SaveAs(destFile);// 파일저장 부분이다 아래주석 부분과 같다
//int i = 0;
//do
//{
// i = fileOpen.ReadByte();
// if (i != -1)
// {
// fd.WriteByte((byte)i);
// }
//} while (i != -1);
//fileOpen.Close();
//fd.Close();
Label1.Text = "원본파일 : " + originfile;
Label2.Text = "복사파일 : " + destFile;
}
디렉터리 및 파일 제어
웹 서버(ASP.NET Development Server가 돌고 있는 여러분의 PC)의 ‘C:\Files2\’ 디렉터리 하위에 존재하는 모든 파일의 이름을 ListBox에 표현하고 <파일 추가> 버튼을 클릭하면 ‘찾아보기…’ 에서 선택한 파일을 ‘C:\Files2\’ 디렉터리에 업로드하며 <선택파일 삭제> 버튼을 클릭하면 ListBox에서 선택한 파일을 삭제하는 웹 페이지를 구현해 봅시다. (힌트 : DirectoryInfo 클래스의 GetFiles() 메서드는 해당 디렉터리에 존재하는 모든 파일 목록을 반환합니다.)
'.NET > ASP.NET' 카테고리의 다른 글
| ASP.NET 데이터소스컨트롤을 사용한 DB연동 SqlDataSource와 Repeater 리핏터 를 사용한 예제 + DataList (0) | 2008.12.09 |
|---|---|
| ASP.NET 리스트컨트롤 예제 (0) | 2008.12.09 |
| ASP.NET 데이터 소스 컨트롤을 사용한 DB 연동 (0) | 2008.12.05 |
| ASP.NET 데이터베이스와 연동하기 , 데이터 소스 컨트롤을 사용한 DB 연동 (0) | 2008.12.05 |
| ASP.NET .NET 프레임워크의 파일 I/O 시스템 바이트 스트림 예제 문자 스트림 , 바이트스트림 (0) | 2008.12.05 |
| ASP.NET 사용자 정의 컨트롤~!! (0) | 2008.12.05 |
| ASP.NET 텍스트 박스 개행 Response.Write(TextBox4.Text.Replace(“\r\n”, “<br>”)); (0) | 2008.12.03 |
| ASP.NET 유효성 검사 컨트롤~!!! RequiredFieldValidator, RangeValidator, CompareValidator , RegularExpressionValidator1,ValidationSummary, (0) | 2008.12.03 |