1

I'm a beginner to the c# language.i just tried some code to save an image in to a folder.but i just wanna change the name of the image using a textbox text and save it to a folder.plese help me!! this is my code..

string appPath = Path.GetDirectoryName(Application.ExecutablePath) + 
@"\IMAGES_DB\"; 

if (Directory.Exists(appPath) == false) 
{                                                                                    
   Directory.CreateDirectory(appPath);                                              }                                                                                    

if (opFile.ShowDialog() == DialogResult.OK) 
{
   try 
   {
      string iName = opFile.SafeFileName;   
      string filepath = opFile.FileName;    

      File.Copy(filepath, appPath + iName); 
      pictureBox2.Image = new Bitmap(opFile.OpenFile());
   }
   catch (Exception exp) 
   {
      MessageBox.Show("Unable to open file " + exp.Message);
   }
}
else
{
    opFile.Dispose();
}
Rehan Shah
  • 1,505
  • 12
  • 28

1 Answers1

0

Well this answer is kinda asked here: Rename a file in C#

So basically use something like this:

System.IO.File.Move("oldfilename", "newfilename");

In your case:

string appPath = Path.GetDirectoryName(Application.ExecutablePath) +
@"\IMAGES_DB\"; 

if (!Directory.Exists(appPath)) {                                                                                    
   Directory.CreateDirectory(appPath);                                              
}                                                                                    

if (opFile.ShowDialog() == DialogResult.OK) {
    try {
        string iName = opFile.SafeFileName.Text;   
        string filepath = opFile.FileName.Text;    

        File.Move(filepath, appPath + iName); 
     } catch (Exception exp) {
        MessageBox.Show("Unable to open file " + exp.Message);
     }
} 
Maarten Kieft
  • 6,806
  • 3
  • 29
  • 34
  • i'm so sorry to say that.....but i don't know how to apply System.IO.File.Move("oldfilename", "newfilename"); THIS code into my code.because i wanna get textbox text and save the image in that name.i feel like i'm a fool...but please help me i'm a very beginner...... – Tharindu Shihan Apr 24 '19 at 06:59
  • Ah, I think you have to use than opFile.SafeFileName.Text and opFile.FileName.Text. As I understood opFile is a form which has 2 textboxes SafeFileName and FileName. The Text property on a textbox contains the inserted value. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox.text?view=netframework-4.8#System_Windows_Forms_TextBox_Text – Maarten Kieft Apr 24 '19 at 07:33
  • What are they than? can you share your code somehow? – Maarten Kieft Apr 24 '19 at 09:10