C#FTP上传问题

时间:2017-11-24 16:38:16

标签: c# ftp

我正在为我的学校制定计划。我刚刚完成,我只需要将我的文件上传到FTP。当我完成代码并测试它时,应用程序处于中断模式。当我删除FTP代码时,它仍然会起作用。 你们中的任何人都有一个线索是错的吗?即时通讯使用core.NET C# 这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace School_0._1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.GetItemText(comboBox1.SelectedItem);
            comboBox2.GetItemText(comboBox1.SelectedItem);
            comboBox3.GetItemText(comboBox1.SelectedItem);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Weet je het zeker?", "Lesbord 1.0", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                MessageBox.Show("Verstuurd");
                //hier de verstuur code
                string path = @"E:\\test.txt";
                string selectedValue = comboBox1.SelectedItem.ToString();
                string selectedValue2 = comboBox2.SelectedItem.ToString();
                string selectedValue1 = comboBox3.SelectedItem.ToString();

                File.WriteAllText(path, selectedValue + " " + selectedValue2 + " " + selectedValue1);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Weet je zeker dat je het programma af wil sluiten?", "Lesbord 1.0", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"E\printscreen.jpeg");
        }
    }
}

这是FTP代码:

private static void Upload(string ftpServer, string userName, string password, string filename)
{
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        client.Credentials = new
        System.Net.NetworkCredential(userName,password);
        client.UploadFile(ftpServer+"/"+newFileInfo(filename).Name,"STOR",filename);
    }
}

2 个答案:

答案 0 :(得分:1)

完成File.WriteAllText后,只需在Upload方法中实施this tuturial中找到的代码并将其调用即可。

private void button1_Click(object sender, EventArgs e)
{
    DialogResult dialogResult = MessageBox.Show("Weet je het zeker?", "Lesbord 1.0", MessageBoxButtons.YesNo);

    if (dialogResult == DialogResult.Yes)
    {
        MessageBox.Show("Verstuurd");
        //hier de verstuur code
        string selectedValue = comboBox1.SelectedItem.ToString();
        string selectedValue2 = comboBox2.SelectedItem.ToString();
        string selectedValue1 = comboBox3.SelectedItem.ToString();

        File.WriteAllText(@"E:\test.txt", selectedValue + " " + selectedValue2 + " " + selectedValue1);

        Upload("ftp://www.myserver.com/", "myuser", "mypass", @"E:\test.txt");
    }
}

我还会实现一个小try... catch块,以便在编写或上传文件时处理潜在的异常,因为我链接到你的示例没有实现任何异常。例如:

private void button1_Click(object sender, EventArgs e)
{
    DialogResult dialogResult = MessageBox.Show("Weet je het zeker?", "Lesbord 1.0", MessageBoxButtons.YesNo);

    if (dialogResult == DialogResult.Yes)
    {
        MessageBox.Show("Verstuurd");
        //hier de verstuur code
        string selectedValue = comboBox1.SelectedItem.ToString();
        string selectedValue2 = comboBox2.SelectedItem.ToString();
        string selectedValue1 = comboBox3.SelectedItem.ToString();

        try {
            File.WriteAllText(@"E:\test.txt", selectedValue + " " + selectedValue2 + " " + selectedValue1);
        } catch (Exception e) {
            MessageBox.Show("Error during file writing!");
            return;
        }

        try {
            Upload("ftp://www.myserver.com/", "myuser", "mypass", @"E:\test.txt");
        } catch (Exception e) {
            MessageBox.Show("Error during file upload!");
            return;
        }
    }
}

实施:

    private static void Upload(string ftpServer, string userName, string 
password, string filename)
    {  
        // Get the object used to communicate with the server.  
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer);  
        request.Method = WebRequestMethods.Ftp.UploadFile;  

        // This example assumes the FTP site uses anonymous logon.  
        request.Credentials = new NetworkCredential(userName, password);  

        // Copy the contents of the file to the request stream.  
        StreamReader sourceStream = new StreamReader(filename);  
        byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());  
        sourceStream.Close();  
        request.ContentLength = fileContents.Length;  

        Stream requestStream = request.GetRequestStream();  
        requestStream.Write(fileContents, 0, fileContents.Length);  
        requestStream.Close();  

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();  
        response.Close();  
    } 

答案 1 :(得分:0)

您只是在写.txt文件... 在执行后尝试File.Save()函数 文件。 Writealltext()