【问题标题】:C# FTP upload problemsC# FTP 上传问题
【发布时间】:2017-11-24 16:38:15
【问题描述】:

我正在为我的学校制定一个计划。我即将完成,我只需要将我的文件上传到 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);
    }
}

【问题讨论】:

  • 我忘记添加了,哎呀!我的错。马上更新
  • 什么是“中断模式”?解释问题。捕捉异常。调试应用程序。在我们为您提供有意义的帮助之前,您错过了许多关键步骤。

标签: c# ftp


【解决方案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();  
    } 

【讨论】:

  • 我在哪里添加位置?在上传命令中还是在私有静态 void 中?
  • 这是你喜欢的。您可以在表单类中的私有静态字符串中定义所有参数(ftp 用户、ftp 通行证、ftp 地址、文件路径),也可以直接将它们写入内联。如果让我选择,我会选择前者。
  • 它可以工作,但它说“应用程序处于中断模式”
  • 它停在字符串 path = @"E\\text.txt" 我是否犯了语法错误?
  • Tep,我的错误我没有修复它:@"E:\test.txt"。
【解决方案2】:

您只是在 .txt 文件中写入... 在
之后尝试 File.Save() 函数 文件。写所有文本()

【讨论】:

  • File.Save 不被接受? (当谈到这些东西时,我有点菜鸟)
猜你喜欢
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
相关资源
最近更新 更多