0

i have created a small ftp program in c# using winforms but when i try to create this in wpf it is showing me an error "Value cannot be null. Parameter name:fileName", The code works fine in winform. here is the code:

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

    }
}
1

1 Answer 1

0

You never set your filePath variable, so it's null. In browser_click you need to do something like

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

Also, you think you have handled your invalid string here:

if (filePath == "")

but at that point filePath = null.

Instead, use if (string.IsEmptyOrNull(filePath))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.