将文件与数据(令牌)一起发送到http服务器,而不指定文件名

时间:2016-09-21 18:08:18

标签: c# http

我使用c#在Windows应用程序中创建FileSystemWatcher,将最新文件发送到HTTP服务器。

1)在此FileSystemWatcher中,单击浏览按钮后,用户可以选择文件夹/目录(将下载最新文件的位置)。

2)点击“监控”按钮后,它将监控从互联网或邮箱下载的新文件。当用户下载任何新文件时,它会触发一个事件(即,用两个按钮指定FileName的消息框,即YES和NO)。

3)除此之外,它还将在文件系统观察器中显示最新文件的完整文件路径。

4)最新文件将出现在目录中(此目录将根据用户的选择而定)并将这些文件与数据一起发送到http服务器。

我的问题是,我如何将这些文件(即从互联网上下载的最新文件)和一些数据(在点击消息框中的“是”按钮后)发送到http服务器,而不指定文件路径。

我可以通过指定本地的完整文件路径来发送文件。但在这种情况下,根据用户的选择,文件路径可能会有所不同。点击“是”按钮后,如何将最新文件(即从互联网或邮箱下载)与一些令牌或数据一起发送到HTTP服务器在c#中。我无法理解它如何选择消息框中显示的文件。

这是我的代码: -

      //This is FileSystemWatcher Created Event 
    private void fileWatcher_Created(object sender,System.IO. FileSystemEventArgs e)
    {
        textBox3.Text += e.ChangeType + ": " + e.FullPath + "\r\n";
        textBox3.Focus();
        textBox3.Select(textBox3.TextLength, 0);
        textBox3.ScrollToCaret();
     }

   //This is FileSystemWatcher Renamed_1Created Event 
     private void fileWatcher_Renamed_1(object sender, RenamedEventArgs e)
    {
       // System.Threading.Thread.Sleep(5000);
        textBox3.Text += e.ChangeType + ": " + e.FullPath+ "\r\n";
        textBox3.Focus();
        textBox3.Select(textBox3.TextLength, 0);
        textBox3.ScrollToCaret();
      try
           {
            System.Threading.Thread.Sleep(3000);
            fileWatcher.EnableRaisingEvents = false;


            DialogResult result = MessageBox.Show(new Form { TopMost = true }, "fileName:-" + e.Name, "New File", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (result == DialogResult.Yes)
               {
                     WebClient client = new WebClient();
                     string myFile = @"FilePath";
                     client.UploadFile("url", "POST", myFile);
                     MessageBox.Show("Sent");
                     client.Dispose();

                //If user click NO button

                    if (result == DialogResult.No)
                 {
                        Close();
                 }
              }
            }

        finally
        {
            fileWatcher.EnableRaisingEvents = true;
        }
     }

    //This is textBox of FileSystemWatcher where the filename and file path will be shown 
     private void textBox3_TextChanged(object sender, EventArgs e)
     {
         textBox3.ScrollBars = ScrollBars.Vertical;
     }

    //This is monitor button
    private void button1_Click(object sender, EventArgs e)
    {
        fileWatcher.Path = @textBox1.Text;
        fileWatcher.Filter = textBox2.Text;
        fileWatcher.IncludeSubdirectories = checkBox1.Checked;
       // fileWatcher.EnableRaisingEvents = true;

        button1.BackColor = Color.DarkGray;
    }

    //This is the stop button
    private void button2_Click(object sender, EventArgs e)
    {
     // fileWatcher.EnableRaisingEvents = false;
        button2.BackColor = Color.DarkGray;
    }

    //This is Browse button
    private void button3_Click(object sender, EventArgs e)
    {
        button3.BackColor = Color.DarkGray;

        // Create a new instance of FolderBrowserDialog.
        FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();


        // A new folder button will display in FolderBrowserDialog.
        folderBrowserDlg.ShowNewFolderButton = true;

        //Show FolderBrowserDialog
        DialogResult dlgResult = folderBrowserDlg.ShowDialog();
        if (dlgResult.Equals(DialogResult.OK))
        {
            //Show selected folder path in textbox1.
            textBox1.Text = folderBrowserDlg.SelectedPath;

            //Browsing start from root folder.
            Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;

        }
    }               

请帮我解决这个问题。 谢谢

0 个答案:

没有答案