Solved

Help with modifying file name before saving to folder.

Posted on 2013-01-15
5
205 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now