-3
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select Student Picture";
ofd.InitialDirectory = @"C:\Picture";
ofd.Filter = "All Files|*.*|JPEGs|*.jpg";
//ofd.Multiselect = false;
if (ofd.ShowDialog()==DialogResult.OK)
{
    if (ofd.CheckFileExists)
    {
        pbStudent.ImageLocation = ofd.FileName;
        string path = ofd.SafeFileName;
        System.IO.File.Copy(ofd.FileName, "/Resources/SImages/" + lblRNo + ".jpg");
    }
}

Can some one help me resolve issue with this error message:

the given path's format is not supported.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Qasim Shaheen
  • 1
  • 1
  • 1
  • 2

3 Answers3

1

Here you use the correct way of formatting the path in Windows with a Backslash

ofd.InitialDirectory = @"C:\Picture";

And in the next line you divert from it

System.IO.File.Copy(ofd.FileName, "/Resources/SImages/" + lblRNo.Text + ".jpg");

just keep the way you did it in the beginning:

System.IO.File.Copy(ofd.FileName, @"\Resources\SImages\" + lblRNo.Text + ".jpg");

One way of avoiding such irritations is to use System.IO.Path.Combine()

string path = System.IO.Path.Combine("Resources", "SImages");

EDIT: due to the extraordinary observation by Steve I changed lblRNo to lblRNo.Text

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
1

Your problem is the lblRNo variable used to express the name of the destination file. It seems that this is a label of your program and not a string containing your file name. To get the correct value you should use the property Text of the label.

System.IO.File.Copy(ofd.FileName, @"/Resources/SImages/" + lblRNo.Text + ".jpg");

Of course, having specified a relative path then you should be sure that this path exists relative to the root drive where your code is executing.
For example, if your code runs on the C: drive then a path

 C:\Resources\SImages 

should exist otherwise you get the execption about a part of your path not being found.

As other have said, the proper path separator in Windows is the backslash but you could also use the forward slash without any problem

Steve
  • 213,761
  • 22
  • 232
  • 286
0

Try this instead:

System.IO.File.Copy(ofd.FileName, @"\Resources\SImages\" + lblRNo + ".jpg");

the / is for the internet, the \ is for inside your computer :)

nozzleman
  • 9,529
  • 4
  • 37
  • 58