Solved

C# Winform File renaming

Posted on 2015-01-29
2
539 Views
Last Modified: 2015-01-29
I have a small Inventory Control winforms project that I'm developing and need help with one aspect. My workflow is this:
1) User selects a SKU Number in a text box (SKUText), and the form is populated with all fields for that record.
2) User uploads an image by selecting a local image to be displayed in a picturebox. The image is uploaded to an FTP server and will be displayed from that path in the picturebox.

What I need help with is that I need to rename the file before the FTP upload so that it matches the SKU number without changing the extension. Im using [ftploc] as the full path and filename to pass to the FTP upload destination to as shown in my code.

Example:  User uploads 'toy_truck.jpg' and I need the filename to become (SKUText.Text).jpg

Thanks the help in advance!

 private void UploadButton_Click(object sender, EventArgs e)
        {
         OpenFileDialog fd = new OpenFileDialog();
         fd.Filter = "Image Files(*.jpg; *.png; *.gif; *.bmp)|*.jpg; *.png; *.gif; *.bmp";
         fd.Title = "Select File for Upload...";          
         if (fd.ShowDialog() == DialogResult.OK)
             {
                                //Need fd.SafeFileName below renamed to text value of SKUText.Text, a textbox further up
				//in my code, without affecting the file extension
				
		     String ftploc = ("ftp://x.x.x.x/" + fd.SafeFileName);           
                     FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftploc);
                     request.Method = WebRequestMethods.Ftp.UploadFile;

                     // FTP Login
                     request.Credentials = new NetworkCredential("domain\\user", "pass");
                     request.Timeout = -1;
                     request.UsePassive = true;
                     request.UseBinary = true;
                     request.KeepAlive = true;
                     request.Proxy = null;

                     // Copy the contents of the file to the request stream.
                     StreamReader sourceStream = new StreamReader(fd.FileName);
                     byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                     sourceStream.Close();
                     request.ContentLength = fileContents.Length;
                     Stream requestStream = request.GetRequestStream();
                     requestStream.Write(fileContents, 0, fileContents.Length);
                     requestStream.Close();

                     FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                     MessageBox.Show("Upload Complete");
                     //ImageBox1.ImageLocation = ftploc;
                     
                     response.Close();
             }
           
        }         

Open in new window

0
Comment
Question by:hhnetworks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
Tapan Pattanaik earned 500 total points
ID: 40577341
HI hhnetworks,

Please use this below code. In your code only you have to retrieve the extension of the file from the path then merge with the value of textbox "SKUText" .

 private void UploadButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Image Files(*.jpg; *.png; *.gif; *.bmp)|*.jpg; *.png; *.gif; *.bmp";
            fd.Title = "Select File for Upload...";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                //Need fd.SafeFileName below renamed to text value of SKUText.Text, a textbox further up
                //in my code, without affecting the file extension                

                String ftploc = ("ftp://x.x.x.x/" + fd.SafeFileName);

                string Extenstion = Path.GetExtension(ftploc);
                string fileName =SKUText.Text.Trim()+Extenstion ;
                ftploc = "ftp://x.x.x.x/" + fileName;             
               
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftploc);
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // FTP Login
                request.Credentials = new NetworkCredential("domain\\user", "pass");
                request.Timeout = -1;
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = true;
                request.Proxy = null;

                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader(fd.FileName);
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                MessageBox.Show("Upload Complete");
                //ImageBox1.ImageLocation = ftploc;

                response.Close();
            }

        }

Open in new window

0
 

Author Closing Comment

by:hhnetworks
ID: 40577353
Worked perfectly!!

Thank you very much!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET Calendar Control 5 25
C# Linq - Join two objects into one 3 33
Dictionary and array of [N] size - performance tuned 12 41
Problem to copy file 14 51
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question