Solved

Help with modifying file name before saving to folder.

Posted on 2013-01-15
5
208 Views
Last Modified: 2013-01-23
Hello,

I am using the code below to modify the filename of an image and assign it to C1Image.text, how do I modify the code to also save the file to my application's folder with the same modified filename? For example image name I want to upload Image1.jpg
I need to save it as Image1_ManufactureNmae_Country.jpg to C1Image.text and also to my application's folder. Problem with the code is it is saving the original filename to my application's folder.

Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath & "\Images", IO.Path.GetFileName(C1Image.Text))
        If IO.File.Exists(DestinationPath) Then
            If MessageBox.Show("Destination already exists!" & vbCrLf & vbCrLf & "File: " & DestinationPath, "Replace File?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                IO.File.Copy(C1Image.Text, DestinationPath, True)
            End If
        Else
            IO.File.Copy(C1Image.Text, DestinationPath)
        End If
        If C1Manufacturer.Text <> "" Then
            C1Image.Text = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName) & "_" & C1Manufacturer.Text & C1CountryOrigin.Text & System.IO.Path.GetExtension(OpenFileDialog1.FileName)
        End If
        If C1Manufacturer.Text = "" Or C1Manufacturer.Text = "Select/Enter" Then
            MsgBox("Manufacturer must be included before saving image")
        End If
        BtnSave.Enabled = False


Thanks,

Victor
0
Comment
Question by:vcharles
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38779986
To rename a file, you need to use the File.Move function by supplying it the old and new names.
0
 

Author Comment

by:vcharles
ID: 38781031
I will try to figure it out and get back to you tomorrow.

Thanks,

V.
0
 

Author Comment

by:vcharles
ID: 38790240
Hi,

I managed to include the extension by moving

C1Image.Text = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName) & "_" & C1Manufacturer.Text & C1CountryOrigin.Text & System.IO.Path.GetExtension(OpenFileDialog1.FileName)

on top of the code.

 but now receiving the following error:

Conversion from string "Application.StartupPath & " to type 'Long' is not valid.

On line

 IO.File.Copy("Application.StartupPath & " \ "Images" \ " + C1Image.Text", DestinationPath)

code:

 C1Image.Text = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName) & "_" & C1Manufacturer.Text & C1CountryOrigin.Text & System.IO.Path.GetExtension(OpenFileDialog1.FileName)

Dim DestinationPath As String = IO.Path.Combine(Application.StartupPath & "\Images", IO.Path.GetFileName(C1Image.Text))
        If IO.File.Exists(DestinationPath) Then
            If MessageBox.Show("Destination already exists!" & vbCrLf & vbCrLf & "File: " & DestinationPath, "Replace File?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                IO.File.Copy(C1Image.Text, DestinationPath, True)
            End If
        Else
            IO.File.Copy(C1Image.Text, DestinationPath)
        End If
        If C1Manufacturer.Text <> "" Then
               End If
        If C1Manufacturer.Text = "" Or C1Manufacturer.Text = "Select/Enter" Then
            MsgBox("Manufacturer must be included before saving image")
        End If
        BtnSave.Enabled = False


How do I fix this error?

Thanks,

Victor
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38792973
Change that line to

 IO.File.Copy(Application.StartupPath & "\Images\" & C1Image.Text, DestinationPath)
0
 

Author Closing Comment

by:vcharles
ID: 38811528
Thank You!
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTML - Color not displaying correctly in EMAIL. 6 49
Problem with SelectList in .NET MVC application 1 27
MailAddress in vb 4 29
How to repeat the data 4 17
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

856 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