Untitled
unknown
plain_text
a year ago
5.6 kB
10
Indexable
private async Task UploadFileToFtpAsync(string localFilePath, string ftpHost, string ftpUsername, string ftpPassword, string folderName, string itemName, string agentName)
{
// Yükleme yapılacak dizini oluştur
string folderUri = string.IsNullOrWhiteSpace(agentName) ? $"{ftpHost}/{folderName}/{itemName}" : $"{ftpHost}/{folderName}/{agentName}/{itemName}";
// Dizini oluştur veya kontrol et
await CreateFtpFolders(folderUri, ftpUsername, ftpPassword);
string fileName = Path.GetFileName(localFilePath);
string fileUri = $"{folderUri}/{fileName}";
try
{
// Dosya yükleme isteğini oluştur
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fileUri);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
request.UseBinary = true;
byte[] fileContents;
using (FileStream sourceStream = new FileStream(localFilePath, FileMode.Open, FileAccess.Read))
{
fileContents = new byte[sourceStream.Length];
await sourceStream.ReadAsync(fileContents, 0, (int)sourceStream.Length);
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = await request.GetRequestStreamAsync())
{
await requestStream.WriteAsync(fileContents, 0, fileContents.Length);
}
using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
{
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
HighlightUploadedFile(fileName); // Dosya yüklendiğinde yeşil yap
}
}
catch (Exception ex)
{
Console.WriteLine($"Upload failed for {fileName}: {ex.Message}");
HighlightFailedFile(fileName);
}
}
private async Task CreateFtpFolders(string folderUri, string username, string password)
{
// URI'yi parçalar
string[] folders = folderUri.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (folders.Length < 2)
{
throw new ArgumentException("folderUri should contain at least two directories.");
}
// FTP host ve ilk dizin ayarla
string basePath = $"ftp://{folders[1]}";
string currentPath = basePath;
for (int i =5; i < folders.Length; i++)
{
currentPath = $"{currentPath}/{folders[2]}/{folders[3]}/{folders[4]}/{folders[i]}/{folders[i]}";
await CreateDirectoryIfNotExists(currentPath, username, password);
}
}
private async Task CreateDirectoryIfNotExists(string directoryPath, string username, string password)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(username, password);
try
{
using (FtpWebResponse response = (FtpWebResponse)await request.GetResponseAsync())
{
Console.WriteLine("Directory created: " + response.StatusDescription);
}
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
// Dizin zaten mevcut
Console.WriteLine("Directory already exists: " + directoryPath);
}
else
{
throw;
}
}
}
private void HighlightUploadedFile(string fileName)
{
fileListBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
fileListBox.DrawItem += (sender, e) =>
{
e.DrawBackground();
Brush brush = fileListBox.Items[e.Index].ToString() == fileName ? Brushes.Green : Brushes.Black;
e.Graphics.DrawString(fileListBox.Items[e.Index].ToString(), e.Font, brush, e.Bounds);
e.DrawFocusRectangle();
};
fileListBox.Invalidate(); // ListBox'u yeniden çiz
count++;
fileUploadInfoLabel.Text = $"Total Number of Files to Upload: {selectedFiles.Count} / {count}";
}
private void HighlightFailedFile(string fileName)
{
fileListBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
fileListBox.DrawItem += (sender, e) =>
{
e.DrawBackground();
Brush brush = fileListBox.Items[e.Index].ToString() == fileName ? Brushes.Red : Brushes.Black;
e.Graphics.DrawString(fileListBox.Items[e.Index].ToString(), e.Font, brush, e.Bounds);
e.DrawFocusRectangle();
};
fileListBox.Invalidate(); // ListBox'u yeniden çiz
fileLodedListBox.Visible = true;
filesNotLoadedLabel.Visible = true;
fileLodedListBox.Items.Add(fileName);
}Editor is loading...
Leave a Comment