C#FTP上传问题

C#FTP上传问题,c#,ftp,C#,Ftp,我正在为我的学校做一个项目。我差不多完成了,我只需要把我的文件上传到FTP。当我完成代码并对其进行测试时,应用程序处于中断模式。当我删除FTP代码时,它仍然可以正常工作。 你们有人知道怎么回事吗?我正在使用core.netc# 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst

我正在为我的学校做一个项目。我差不多完成了,我只需要把我的文件上传到FTP。当我完成代码并对其进行测试时,应用程序处于中断模式。当我删除FTP代码时,它仍然可以正常工作。 你们有人知道怎么回事吗?我正在使用core.netc# 这是我的密码:

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);
    }
}

您只是在.txt文件中写入。。。 在

文件Writealltext()

处理完
文件后。Writealltext
只需实现在
上载方法中找到的代码并调用它即可

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");
    }
}
我还将实现一点
尝试。。。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();  
    } 

我忘记加了,哎呀!我的错误。现在将更新它什么是“中断模式”?解释问题。捕捉异常。调试应用程序。在我们有意义地帮助您之前,您错过了很多关键步骤。File.Save不被接受?(说到这些东西,我有点不知所措)我该在哪里添加位置?在upload命令中还是在private static void中?这是您喜欢的。您可以在表单类中的私有静态字符串中定义所有参数(ftp用户、ftp传递、ftp地址、文件路径),也可以直接内联编写它们。如果让我选择前者,我会选择前者。它可以工作,但它说“应用程序处于中断模式”,它在字符串路径上暂停=@“E\\text.txt”我有语法错误吗?Tep,我的错误我没有修复:@“E:\test.txt”。