Jeg har fået det til at virke.
Her er koden:
private void button6_Click(object sender, EventArgs e) //Test
// public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass).
{
// Koden er taget fra
//
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Upload-file-to-FTP-Server.html // <summary>.
// Methods to upload file to FTP Server.
// Ssummary.
// _FileName = local source file name.
// _UploadPath = FTP path including Host name.
// _FTPUser = FTP login username.
// _FTPPass = FTP login password.
string _FileName = "C:\\sombok\\som_kalender_2012.txt" ;
string _UploadPath = "
ftp://broma.dk/public_html/sommerhus/som_kalender_2012.txt" ;
string _FTPUser = "broma.dk" ;
string _FTPPass = "asd12asd" ;
System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName);
// Create FtpWebRequest object from the Uri provided.
System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath));
// Provide the WebPermission Credintials.
_FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed..
_FtpWebRequest.KeepAlive = false;
// set timeout for 20 seconds.
_FtpWebRequest.Timeout = 20000;
// Specify the command to be executed..
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type..
_FtpWebRequest.UseBinary = true;
// Notify the server about the size of the uploaded file.
_FtpWebRequest.ContentLength = _FileInfo.Length;
// The buffer size is set to 2kb.
int buffLength = 2048;
byte[] buff = new byte[buffLength];
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded.
System.IO.FileStream _FileStream = _FileInfo.OpenRead();
try
{
// Stream to which the file to be upload is written.
System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();
// Read from the file stream 2kb at a time.
int contentLen = _FileStream.Read(buff, 0, buffLength);
// Till Stream content ends.
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream.
_Stream.Write(buff, 0, contentLen);
contentLen = _FileStream.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream.
_Stream.Close();
_Stream.Dispose();
_FileStream.Close();
_FileStream.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Koden stod pænt indrykket da jeg copyede, men efter paste er det rodet.
Jeg håber det kan læses.
Som tidligere sagt har jeg ikke skrevet det rigtige password.
Alle der har brugt tid på det, smid et svar, så giver jeg point.
MVH
Bjarne