2013-02-20 FTP 파일 업로더
간단하게 FTP 서버에 있는 텍스트 파일을 수정하기 위하여 만들었던 프로그램입니다.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
namespace FTPListUpload
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(String section, String key, String def, StringBuilder retVal, int size, String filePath);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (!File.Exists("setting.ini"))
{
MessageBox.Show("설정 파일이 존재하지 않습니다. (setting.ini)", "존재하지 않음!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
Application.Exit();
}
try
{
WebClient wc = new WebClient();
Stream data = wc.OpenRead("http://" + IniReadValue("site") + IniReadValue("path"));
StreamReader read = new StreamReader(data);
List<string> lines = new List<string>();
string nextLine = read.ReadLine();
while (nextLine != null)
{
lines.Add(nextLine);
nextLine = read.ReadLine();
}
textBox1.Lines = lines.ToArray();
}
catch (Exception) { FileWrite("FTP 서버에 접근하지 못했습니다."); Application.Exit(); }
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
string ftpfullpath = "ftp://" + IniReadValue("site") + IniReadValue("ftppath");
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(IniReadValue("username"), IniReadValue("password"));
ftp.KeepAlive = false;
ftp.UseBinary = true;
ftp.UsePassive = false;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
ftp.Abort();
}
catch (Exception) { FileWrite("FTP 서버에 파일을 업로드 하지 못했습니다."); }
}
private void FileWrite(string str)
{
FileStream fs = new FileStream("progLog.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
sw.WriteLine("[" + DateTime.Now.ToString("yyyy/MM/dd h:mm tt") + "] " + str);
sw.Flush();
sw.Close();
fs.Close();
}
public String IniReadValue(String Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString("setting", Key, "", temp, 255, "./setting.ini");
return temp.ToString();
}
}
}
'소스 관련' 카테고리의 다른 글
| C# 화면 흘러내림(Screen melt) (1) | 2013.03.24 |
|---|---|
| 2013-02-20 C# 마인크래프트 사이트 로그인 (3) | 2013.02.20 |
| 비주얼 베이직 6.0 멀티파트 관련 클래스 모듈 (0) | 2013.02.14 |
| C# 네이버 메일 이미지 업로드 코드 (0) | 2013.02.09 |
| Visual Basic 6으로 만들어진 추첨기 코드 (0) | 2013.02.05 |