TB Gamezz TB Gamezz - 10 months ago 88
C# Question

C# FTP upload problems

im working on a program for my school. i am just about done, i only needed to upload my file to an FTP. when i finished the code, and tested it, the application was put in break mode. when i removed the FTP code, it still would function.
do any of you have a clue whats wrong? im using core.NET C#
here's my code:

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


}
and here is the FTP code:

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

Answer Source

When you are done with your File.WriteAllText, just implement the code you find in this tuturial in your Upload method and call it.

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

I would also implement a little try... catch block in order to handle potential exceptions when writing or uploading the file, since the example I linked to you doesn't implement any. For example:

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

The implementation:

    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,"[email protected]");  

        // 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();  
    } 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download