WPF FTP操作

Site Index




FTPサーバー内容表示
private void button_FTP_Click(object sender, RoutedEventArgs e)
{
Uri w_uri = new Uri(@z_FTP_dir_data);
//FtpWebRequestの作成
System.Net.FtpWebRequest w_ftpRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(w_uri);
w_ftpRequest.Credentials = new System.Net.NetworkCredential(z_FTP_id,z_FTP_password);

//MethodにWebRequestMethods.Ftp.ListDirectoryDetails("LIST")を設定
w_ftpRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;
w_ftpRequest.KeepAlive = false;
w_ftpRequest.UsePassive = false;

//FtpWebResponseを取得
System.Net.FtpWebResponse w_ftpRes = (System.Net.FtpWebResponse)w_ftpRequest.GetResponse();
//FTPサーバーから送信されたデータを取得
System.IO.StreamReader w_stream = new System.IO.StreamReader(w_ftpRes.GetResponseStream(),System.Text.Encoding.GetEncoding("Shift_JIS"));
string w_res = w_stream.ReadToEnd();
//ファイル一覧を表示
Console.WriteLine(w_res);

w_stream.Close();

//FTPサーバーから送信されたステータスを表示
Console.WriteLine("status-->{0}:::Description-->{1}", w_ftpRes.StatusCode, w_ftpRes.StatusDescription);

w_ftpRes.Close();
//
//項目別書き出し
//
FileStruct[] w_fileList = (new ParseListDirectory()).GetList(w_res);
Console.WriteLine("------------After Parsing-----------");

z_dataTable_FTP.Clear();
z_dataTable_FTP.Columns.Clear();
z_dataTable_FTP.Columns.Add("...");
z_dataTable_FTP.Columns.Add("名称");
//z_dataTable_FTP.Columns.Add("Owner");
z_dataTable_FTP.Columns.Add("属性");
z_dataTable_FTP.Columns.Add("作成");
DataRow w_DataRow;
int w_count = 0;

foreach (FileStruct w_struct in w_fileList)
{
w_DataRow = z_dataTable_FTP.Rows.Add();
if (w_struct.IsDirectory)
{
w_DataRow["..."] = "

"; w_DataRow["名称"] = w_struct.Name; w_DataRow["属性"] = w_struct.Flags; w_DataRow["作成"] = w_struct.CreateTime; Console.WriteLine(" " + w_struct.Name + "," + w_struct.Owner + "," + w_struct.Flags + "," + w_struct.CreateTime); } else { //string w_str = "シフトJISへ変換"; string w_str = w_struct.Name; Encoding w_sjisEnc = Encoding.GetEncoding("Shift_JIS"); byte[] w_sjis_bytes = w_sjisEnc.GetBytes(w_str); string w_sjis_str = w_sjisEnc.GetString(w_sjis_bytes); //Console.WriteLine(BitConverter.ToString(bytes)); w_DataRow["..."] = ""; //w_DataRow["名称"] = thisstruct.Name; w_DataRow["名称"] = w_struct.Name; w_DataRow["属性"] = w_struct.Flags; w_DataRow["作成"] = w_struct.CreateTime; Console.WriteLine("" + w_struct.Name + "," + w_struct.Owner + "," + w_struct.Flags + "," + w_struct.CreateTime); } w_count++; } this.dataGrid_FTP.ItemsSource = z_dataTable_FTP.DefaultView; this.dataGrid_FTP.FontSize = 12; this.dataGrid_FTP.SelectedIndex = 0; }

FTPサーバーへのアップロード
private void proc_Upload()
{
    WebClient w_FTP = new WebClient();
    w_FTP.Credentials = new NetworkCredential(z_FTP_id,z_FTP_password);

    var w_itemsSource = this.dataGrid_FileInfo.ItemsSource as IEnumerable;
    //if (null == w_itemsSource) yield return null;
    foreach (var w_item in w_itemsSource)
    {
        DataRowView w_Row = (DataRowView)w_item;
        Boolean w_check = Convert.ToBoolean(w_Row.Row.ItemArray[0]);
        string w_path = w_Row.Row.ItemArray[1].ToString();
        string w_path_FTP = w_path.Replace('\\', '/');

        //string w_path1 = @"c:\windows\system32\notepad.exe";
        string w_fileName = System.IO.Path.GetFileName(w_path);

        if (w_check)
        {
            w_FTP.UploadFile(z_FTP_dir_data + w_fileName, w_path_FTP);
            MessageBox.Show(w_fileName);
            //表題
            string w_dir = System.IO.Path.GetDirectoryName(w_path_FTP);
            string w_name = System.IO.Path.GetFileNameWithoutExtension(w_path_FTP);
            string w_ext = System.IO.Path.GetExtension(w_path_FTP);
            string w_HosokuFile = w_dir + "/text/" + w_name + ".txt";
            MessageBox.Show(w_HosokuFile);
            try
            {
                w_FTP.UploadFile(z_FTP_dir_text + w_name + ".txt", w_HosokuFile);
            }
            catch { }
        }
    }
}

FTPサーバーファイル削除
private void proc_Delete()
{
    string w_dataName = z_FTP_dir_data + this.textBlock_dir_FTP.Text;
    string w_name = System.IO.Path.GetFileNameWithoutExtension(w_dataName);

    string w_textName = z_FTP_dir + "text/" + w_name + ".txt";
    MessageBox.Show(w_textName);

    Uri w_uri1 = new Uri(w_dataName);
    System.Net.FtpWebRequest ftpReq1 = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(w_uri1);
    ftpReq1.Credentials = new System.Net.NetworkCredential(z_FTP_id, z_FTP_password);
    ftpReq1.Method = System.Net.WebRequestMethods.Ftp.DeleteFile;
    System.Net.FtpWebResponse ftpRes1 = (System.Net.FtpWebResponse)ftpReq1.GetResponse();
    ftpRes1.Close();
    //
    Uri w_uri2 = new Uri(w_textName);
    System.Net.FtpWebRequest ftpReq2 = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(w_uri2);
    ftpReq2.Credentials = new System.Net.NetworkCredential(z_FTP_id, z_FTP_password);
    ftpReq2.Method = System.Net.WebRequestMethods.Ftp.DeleteFile;
    try
    {
        System.Net.FtpWebResponse ftpRes2 = (System.Net.FtpWebResponse)ftpReq2.GetResponse();
        ftpRes2.Close();
    }
    catch { }
    MessageBox.Show("削除されました");
}

レジストリ読込
private void proc_init()
{
    z_timer.Tick += new EventHandler(w_timer_Tick);

    Microsoft.Win32.RegistryKey w_register =
        Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Wpf-FTP\DIR");
    z_dir_user1 = (string)w_register.GetValue("user1");
    z_dir_user2 = (string)w_register.GetValue("user2");
    z_FTP_dir = (string)w_register.GetValue("FTP");
    this.button_user1.ToolTip = z_dir_user1;
    this.button_user2.ToolTip = z_dir_user2;
    this.button_FTP.ToolTip = z_FTP_dir;
    this.button_Upload.ToolTip = z_FTP_dir;
    this.textBlock_dir_FTP.Text = z_FTP_dir;
    w_register.Close();
    z_FTP_dir_data = z_FTP_dir + "data/";
    z_FTP_dir_text = z_FTP_dir + "text/";
}

レジストリ書込
private void button_Write_Click(object sender, RoutedEventArgs e)
{
    string w_regKey = this.textBlock_regKey.Text;
    string w_regData = this.textBox_regData.Text;
    Microsoft.Win32.RegistryKey w_register =
        Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Wpf-FTP\DIR");
    w_register.SetValue(w_regKey, @w_regData);
    w_register.Close();
    MessageBox.Show("変更を保存しました");
    this.Close();
}

Windows標準フォルダ
private void button_dir2_Click(object sender, RoutedEventArgs e)
{
    z_LocalDIR = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    proc_DSP_Local();
}
private void button_dir3_Click(object sender, RoutedEventArgs e)
{
    z_LocalDIR = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    proc_DSP_Local();
}
private void button_dir4_Click(object sender, RoutedEventArgs e)
{
    z_LocalDIR = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);
    proc_DSP_Local();
}

Window間データ渡し(A)
private void proc_Rename()
{
    TextBlock w_textBlock = this.textBlock_imageID;
    Window2 w_window = new Window2(w_textBlock.Text);
    w_window.ShowDialog();
    z_newName = w_window.Global_NewName;
    if (w_window.Global_Param == true)
    {
        //MessageBox.Show("変更します");
        string w_app = System.Reflection.Assembly.GetExecutingAssembly().Location;
        string w_dir = System.IO.Path.GetDirectoryName(w_app);

        //MessageBox.Show(w_dir);

        BitmapImage w_image = new BitmapImage();
        w_image.BeginInit();
        w_image.UriSource = new Uri(w_dir + "/image-1.png");
        this.image_Select.Source = w_image;
        w_image.EndInit();

        MessageBox.Show("ファイル名変更 -1- リソース解放");

        BitmapImage w_image2 = new BitmapImage();
        w_image2.BeginInit();
        w_image2.UriSource = new Uri(w_dir + "/image-2.png");
        this.image_Select.Source = w_image2;
        w_image2.EndInit();

        proc_StartTimer();

        //BitmapImage w_image2 = new BitmapImage();
        //w_image2.BeginInit();
        //w_image2.UriSource = new Uri(w_dir + "/image-2.png");
        //this.image_Select.Source = w_image2;
        //w_image2.EndInit();

        //MessageBox.Show("ファイル名変更 -2- 名称変更");

        //string w_nameFrom = this.textBlock_imageID.Text;
        //string w_nameTo = w_window.Global_NewName;
        //System.IO.File.Move(w_nameFrom, w_nameTo);
        //proc_DSP_Local();

        //MessageBox.Show("ファイル名が変更されました。");
    }
    else
    {
        MessageBox.Show("キャンセルされました。");
    }
}
private void proc_StartTimer()
{
    z_timer.Interval = new TimeSpan(0, 0, 1);
    z_timer.Start();
}
void w_timer_Tick(object sender, EventArgs e)
{
    string w_app = System.Reflection.Assembly.GetExecutingAssembly().Location;
    string w_dir = System.IO.Path.GetDirectoryName(w_app);

    z_timer.Stop(); 
    MessageBox.Show("ファイル名変更 -2- 名称変更");

    string w_nameFrom = this.textBlock_imageID.Text;
    string w_nameTo = z_newName;
    System.IO.File.Move(w_nameFrom, w_nameTo);
    proc_DSP_Local();
    MessageBox.Show("ファイル名が変更されました。");
}

Window間データ渡し(B)
private Boolean _Global_Param;
public Boolean Global_Param
{
    get
    {
        return _Global_Param;
    }
    set
    {
        _Global_Param = value;
    }
}
private String _Global_NewName;
public String Global_NewName
{
    get
    {
        return _Global_NewName;
    }
    set
    {
        _Global_NewName = value;
    }
}
String z_FromName = null;
public Window2(String p_fullName)
{
    InitializeComponent();
    this.textBlock_FullName.Text = p_fullName;
    z_FromName = p_fullName;
    proc_init();
}
private void proc_init()
{
    string w_dir = System.IO.Path.GetDirectoryName(z_FromName);
    string w_name = System.IO.Path.GetFileNameWithoutExtension(z_FromName);
    string w_ext = System.IO.Path.GetExtension(z_FromName);

    this.textBlock_FullName.Text = z_FromName;
    this.textBlock_dir.Text = w_dir;
    this.textBox_FileName.Text = w_name;
    this.textBlock_ext.Text = w_ext;
}
private void textBox_FileName_TextChanged(object sender, TextChangedEventArgs e)
{
    string w_nameTo = this.textBlock_dir.Text + "\\" + this.textBox_FileName.Text + this.textBlock_ext.Text;
    this.textBlock_Update.Text = w_nameTo;
}
private void button_uodate_Click(object sender, RoutedEventArgs e)
{
    string w_nameFrom = this.textBlock_FullName.Text;
    string w_nameTo = this.textBlock_Update.Text;
    //System.IO.File.Move(w_nameFrom, w_nameTo);
    Global_NewName = w_nameTo;
    Global_Param = true;
    this.Close();
}
private void button_cansel_Click(object sender, RoutedEventArgs e)
{
    Global_Param = false;
    this.Close();
}

AX