簡體   English   中英

無法從C#(WPF)FTP上傳文件

[英]Can not upload file from C# (WPF) FTP

我已經使用Winforms在C#中創建了一個小型ftp程序,但是當我嘗試在wpf中創建它時,它向我顯示錯誤“值不能為空。參數名稱:fileName”,該代碼在winform中可以正常工作。 這是代碼:

public partial class Ftp : UserControl
{
    string filePath;
    public Ftp()
    {
        InitializeComponent();
    }
    //Clear TextBox when clicked
    #region textBox clear
    public void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        //TextBox tb = (TextBox)sender;
        //tb.Text = string.Empty;
        //tb.GotFocus -= TextBox_GotFocus;
    }
    #endregion

    //Open File Dialog
    private void browser_click(object sender, RoutedEventArgs e)
    {
        #region File Dialog
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.CheckFileExists = true;
        dlg.DefaultExt = ".txt";
        dlg.Filter = "JPEG Files (*.txt)|*.txt|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
        dlg.FileName = "";

        Nullable<bool> result = dlg.ShowDialog();

        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            pathtext.Text = filename;
        }
        #endregion
    }

    //Upload the File to FTP
    private void Upload_click(object sender, RoutedEventArgs e)
    {
        #region Upload files
        if (filePath == "")
        {
            MessageBox.Show("Please Select The File To Upload..");
        }
        else
        {
            browser.IsEnabled = false;
            try
            {
                FileInfo fileInfo = new FileInfo(filePath);
                string fileName = fileInfo.Name.ToString();
                WebRequest requestFTP = WebRequest.Create("ftp://" + hosttext.Text + "/" + fileName);
                requestFTP.Credentials = new NetworkCredential(usertext.Text, passtext.Password);
                requestFTP.Method = WebRequestMethods.Ftp.UploadFile;
                FileStream fStream = fileInfo.OpenRead();
                int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];

                Stream uploadStream = requestFTP.GetRequestStream();
                int contentLength = fStream.Read(buffer, 0, bufferLength);

                while (contentLength != 0)
                {
                    uploadStream.Write(buffer, 0, contentLength);
                    contentLength = fStream.Read(buffer, 0, bufferLength);
                }
                uploadStream.Close();
                fStream.Close();
                requestFTP = null;
                MessageBox.Show("File Uploading Is SuccessFull...");
            }
            catch (Exception ep)
            {
                MessageBox.Show("ERROR: " + ep.Message.ToString());

            }
            browser.IsEnabled = true;
            filePath = "";
            hosttext.Text = usertext.Text = passtext.Password = pathtext.Text = "";
        }
        #endregion

    }
}

您永遠不會設置filePath變量,因此它為null。 browser_click您需要執行以下操作

if (result == true)
{
    // Open document 
    string filename = dlg.FileName;
    filePath = filename;           //added this line
    pathtext.Text = filename;
}

另外,您認為您已經在此處處理了無效的字符串:

if (filePath == "")

但是那時filePath = null

而是使用if (string.IsEmptyOrNull(filePath))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM