Вызов функции из кнопки формы приложения Visual Studio - PullRequest
0 голосов
/ 09 марта 2020

Я пытаюсь объединить 2 кода: один - это приложение для формы, где я получаю некоторую информацию, а другой - запрос Amazon на ведро. У меня возникли проблемы с вызовом функции AWS () из Program.cs в Form1.cs и получением информации, когда я нажимаю кнопку формы.

FORM1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
    private string accessKey;

    private string GetAccessKey()
    {
        return accessKey;
    }

    private void SetAccessKey(string value)
    {
        accessKey = value;
    }

    private string privatekey;

    private string GetPrivatekey()
    {
        return privatekey;
    }

    private void SetPrivatekey(string value)
    {
        privatekey = value;
    }

    private string region;

    private string GetRegion()
    {
        return region;
    }

    private void SetRegion(string value)
    {
        region = value;
    }

    private string bucketname;

    private string GetBucketname()
    {
        return bucketname;
    }

    private void SetBucketname(string value)
    {
        bucketname = value;
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label4_Click(object sender, EventArgs e)
    {

    }

    private void textBox_AccessKey_TextChanged(object sender, EventArgs e)
    {

    }

    private void textbox_PrivateKey_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox_Region_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBoxBucketName_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SetAccessKey(textBox_AccessKey.Text);
        SetPrivatekey(textbox_PrivateKey.Text);
        SetRegion(textBox_Region.Text);
        SetBucketname(textBoxBucketName.Text);
        AWS(GetAccessKey(),GetPrivateKey(),GetBucketname());
    }
}

}

-------------------- \ ----------------- ---- \ -----------

Program.CS

using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Transfer;
using System;
using System.IO;
using System.Threading.Tasks;
using Amazon;

namespace Amazon.DocSamples.S3
{
  class MakeS3RequestTest
  {
     static string accessKey = "******";
     static string secretKey = "********";
     private const string bucketName = "******";
     // Specify your bucket region (an example region is shown).
     private static readonly RegionEndpoint bucketRegion = RegionEndpoint.******;
     private static AmazonS3Client client = new AmazonS3Client(accessKey, secretKey, bucketRegion);
     private static TransferUtility fileTransferUtility = new TransferUtility(client);
     private static string directoryPath = "*******";


     public static async Task AWS(string accesskey, string privatekey, string bucketname)
     {

         using (client)
         {
             Console.WriteLine("Listing objects stored in a bucket");
             ListObjectsRequest request = new ListObjectsRequest();
             ListObjectsResponse response = await client.ListObjectsAsync(request.BucketName = bucketName);
            // Process the response.
            foreach (S3Object obj in response.S3Objects)
            {
                Console.WriteLine("key = {0}", obj.Key);
                try
                {
                    string filename = directoryPath + "\\" + obj.Key;
                    FileStream fs = File.Create(filename);
                    fs.Close();
                    fileTransferUtility.Download(filename, bucketName, obj.Key);

                }
                catch (Exception Excep)
                {
                    Console.WriteLine(Excep.Message, Excep.InnerException);
                }
            }
        }
    }
}

}

1 Ответ

1 голос
/ 09 марта 2020

Я заметил, что вы определили метод AWS в классе MakeS3RequestTest, и это метод stati c.

Так что вы можете вызвать его, нажав кнопку с помощью следующего кода.

private void button1_Click(object sender, EventArgs e)
{
    MakeS3RequestTest.AWS(/*params*/);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...