Question

I have read a csv file from a filedialog in window application.

No i wish to copy a particular file to a folder, which is generating a problem.

the code is as follows

OpenFileDialog op1 = new OpenFileDialog();
op1.Multiselect = false;
op1.ShowDialog();
op1.Filter = "allfiles|*.csv";
txtSearchName.Text = op1.FileName;


File.Copy(op1.FileName, "C:\\Users\\skysoft\\Documents\visual studio 2010\\Projects\\MailSend\\MailSend\\CsvFile\\" + op1.FileName);

what i am doing wrong in it please help.

Was it helpful?

Solution

i would recommend always use the @ sign before path strings to avoid problems like yours: you need 1 more back slash before "visual studio 2010". for example:

@"C:\Users\skysoft\Documents\visual studio 2010\Projects\MailSend\MailSend\CsvFile\"

also, you are giving the method

"C:\\Users\\skysoft\\Documents\visual studio 2010\\Projects\\MailSend\\MailSend\\CsvFile\\" + op1.FileName

which translate to

"C:\\Users\\skysoft\\Documents\visual studio 2010\\Projects\\MailSend\\MailSend\\CsvFile\\" + "c:\\...."

you should do Path.GetFileName(op1.FileName) as keyboardP said or op1.FileName.Substring(op1.LastIndexOf('\\'))

OTHER TIPS

Check the string, theres a missing backslash \ before visual studio

File.Copy(op1.FileName, "C:\\Users\\skysoft\\Documents\visual studio 2010\\Projects\\MailSend\\MailSend\\CsvFile\\" + op1.FileName);

op1.FileName contains the full path whereas you just want the filename when appending it to your destination directory.

using(OpenFileDialog op1 = new OpenFileDialog())
{
    op1.Multiselect = false;        
    op1.Filter = "allfiles|*.csv";
    op1.ShowDialog();
    txtSearchName.Text = op1.FileName;

    string dest = Path.Combine(@"C:\Users\skysoft\Documents\visual studio 2010\Projects\MailSend\MailSend\CsvFile\", Path.GetFileName(op1.FileName));
    File.Copy(op1.FileName, dest);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top