0

我已经使用 winforms 在 c# 中创建了一个小型 ftp 程序,但是当我尝试在 wpf 中创建它时,它向我显示一个错误“值不能为空。参数名称:文件名”,该代码在 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

    }
}
4

1 回答 1

0

你从来没有设置你的filePath变量,所以它是空的。在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))

于 2013-07-14T15:04:50.803 回答