I'm using thread but when uploading file.Not display filename on label.I calling Upload() method on foreach loop.
public void Upload(string filepath, string targetpath)
{
Thread thread; = new Thread(() => AktarilanDosyaYaz(filepath));
thread.Start();
//thread1 çalıştırılır
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(FtpUser, FtpPass);
client.Proxy = null;
var fixedpath = targetpath.Replace(@"\", "/");
client.UploadFile( FtpServerUrl + "/" + fixedpath, WebRequestMethods.Ftp.UploadFile, filepath);
}
}
This Displaying metod.AktarilanDosyaYaz() method %100 working.I was try method with button.But not working calling on Upload() method
Form1 frm = System.Windows.Forms.Application.OpenForms.OfType<Form1>().SingleOrDefault();
public void AktarilanDosyaYaz(string dosyaYolu)
{
frm.label9.Text ="Aktarılan Dosya : "+ dosyaYolu;
}
Please help me friends
Windows Form Runtime ScreenShot
MyFtpClient.cs with Breakpoint and value display on variable ScreenShot
All Code - Form1.cs -Call MyFtpClient
MyFtpClient ftp = new MyFtpClient(ftp_username, ftp_sifre, ftpServerUrl, ftp_klasor_yolu,firma_id);
ftp.UploadDirectory();
All Code - MyFtpClient
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Barge_Setup
{
class MyFtpClient
{
protected string FtpUser { get; set; }
protected string FtpPass { get; set; }
protected string FtpServerUrl { get; set; }
protected string DirPathToUpload { get; set; }
protected string BaseDirectory { get; set; }
protected string FirmaKlasoru { get; set; }
Form1 frm = System.Windows.Forms.Application.OpenForms.OfType<Form1>().SingleOrDefault();
public void AktarilanDosyaYaz(string dosyaYolu)
{
try
{
System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
frm.label9.Text = "Aktarılan Dosya : " + dosyaYolu;
}
catch (Exception e)
{
string hata = e.Message;
}
}
public MyFtpClient(string ftpuser, string ftppass, string ftpserverurl, string dirpathtoupload,string firma_klasoru)
{
this.FtpPass = ftppass;
this.FtpUser = ftpuser;
this.FtpServerUrl = ftpserverurl;
this.DirPathToUpload = dirpathtoupload;
var spllitedpath = dirpathtoupload.Split('\\').ToArray();
// last index must be the "base" directory on the server
this.BaseDirectory = spllitedpath[spllitedpath.Length - 1];
this.FirmaKlasoru = firma_klasoru;
}
public void UploadDirectory()
{
// rename the old folder version (if exist)
// create a parent folder on server
CreateDir(BaseDirectory);
// upload the files in the most external directory of the path
UploadAllFolderFiles(DirPathToUpload, BaseDirectory);
// loop trough all files in subdirectories
foreach (string dirPath in Directory.GetDirectories(DirPathToUpload, "*",
SearchOption.AllDirectories))
{
// create the folder
Thread thread1 = new Thread(() => CreateDir(dirPath.Substring(dirPath.IndexOf(BaseDirectory), dirPath.Length - dirPath.IndexOf(BaseDirectory))));
thread1.Start();
Thread thread2 = new Thread(() => UploadAllFolderFiles(dirPath, dirPath.Substring(dirPath.IndexOf(BaseDirectory), dirPath.Length - dirPath.IndexOf(BaseDirectory))));
thread2.Start();
}
Thread thread4 = new Thread(() => RenameDir(BaseDirectory, FirmaKlasoru));
thread4.Start();
}
private void UploadAllFolderFiles(string localpath, string remotepath)
{
string[] files = Directory.GetFiles(localpath);
// get only the filenames and concat to remote path
foreach (string file in files)
{
// full remote path
var fullremotepath = remotepath + "\\" + Path.GetFileName(file);
// local path
var fulllocalpath = Path.GetFullPath(file);
// upload to server
int indexOf = fulllocalpath.IndexOf("FTP_V3");
string threadPatch = "";
if (fulllocalpath.EndsWith("FTP_V3")==true)
{
threadPatch = fulllocalpath;
}
else
{
threadPatch = fulllocalpath.Substring(indexOf+7, fulllocalpath.Length-(indexOf + 7));
}
// last index must be the "base" directory on the server
Thread thread3 = new Thread(() => AktarilanDosyaYaz(threadPatch));
thread3.Start();
//thread1 çalıştırılır
//Upload(fulllocalpath, fullremotepath);
}
}
public bool CreateDir(string dirname)
{
try
{
WebRequest request = WebRequest.Create(FtpServerUrl + "/" + dirname);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Proxy = new WebProxy();
request.Credentials = new NetworkCredential(FtpUser, FtpPass);
using (var resp = (FtpWebResponse)request.GetResponse())
{
if (resp.StatusCode == FtpStatusCode.PathnameCreated)
{
return true;
}
else
{
return false;
}
}
}
catch
{
return false;
}
}
public void Upload(string filepath, string targetpath)
{
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(FtpUser, FtpPass);
client.Proxy = null;
var fixedpath = targetpath.Replace(@"\", "/");
client.UploadFile( FtpServerUrl + "/" + fixedpath, WebRequestMethods.Ftp.UploadFile, filepath);
}
}
public bool RenameDir(string dirname,string toDirName)
{
var path = FtpServerUrl + "/" + dirname;
string serverUri = path;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.Rename;
request.Proxy = null;
request.Credentials = new NetworkCredential(FtpUser, FtpPass);
// change the name of the old folder the old folder
request.RenameTo = toDirName;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (var resp = (FtpWebResponse)request.GetResponse())
{
if (resp.StatusCode == FtpStatusCode.FileActionOK)
{
return true;
}
else
{
return false;
}
}
}
catch
{
return false;
}
}
}
}