0

I am writing a program in C# which has to: * make the user choose a file from a 'browse-screen' for example "C:/temp/DirectoryExample/FileExample.xxx" * save the chosen file as the name of the directory it was chosen from (in this case DirectoryExample.xxx) in a location that is pre-set by the program.

i have tried using these options to accomplish this:

if (sfdGekozenBestand.ShowDialog() == DialogResult.OK)
{
    tbGekozenBestand.Text = sfdGekozenBestand.FileName;
    tbVeranderNaamIn.Text = f_sNieuweNaam;
    File.Copy(tbVeranderNaamIn.Text, f_sNieuweNaam, true);
}

Or

if (sfdGekozenBestand.ShowDialog() == DialogResult.OK)
{
    tbGekozenBestand.Text = sfdGekozenBestand.FileName;
    tbVeranderNaamIn.Text = f_sNieuweNaam;
    f_srStreamReader = new StreamReader(tbGekozenBestand.Text);
    f_swStreamwriter = new StreamWriter(f_sNieuweNaam);
}

Here is the thing, when i debug it in Visual studio 2010. I click the save button in the SaveFileDialog, it asks me if want to overwrite the currently existing file, I click Yes. then it crashes and gives me this:

IOExeption was unhandled.

Can't get access to the file C:/temp/DirectoryExample.xxx because it is in use by another proces

the troubleshooting tips are worthless, and my google-using skills are coming in short.

Any help would be appriciated.

[EDIT]

Fixed with How to copy a file while it is being used by other process

Community
  • 1
  • 1
Cas Nouwens
  • 395
  • 1
  • 5
  • 25
  • 1
    Is the file at `C:/temp/DirectoryExample.xxx` opened by another tool, like Notepad or Visual Studio itself? – Karl Anderson Sep 02 '13 at 07:37
  • 1
    Do you open the file before it? if yes, verify you perfrom Close() – AsfK Sep 02 '13 at 07:38
  • @KarlAnderson well, I am not sure, i checked with an process explorer and it didn't show up in the list. i don't put the file in a stream or anything. when i select it in the 'Browse' screen Visual Studio might reserve it for some reason. but i can't tell – Cas Nouwens Sep 02 '13 at 07:43
  • 1
    Even though it says "another process", what it really means is "it's already open". So it could be your own program that has it open. – Matthew Watson Sep 02 '13 at 07:47

0 Answers0