-1

I have an Error if I write something in a newly created File.

This is my code:

private void ButtonClick(object sender, EventArgs e)
    {                    
        Button b = (Button)sender;
        string inputKey = b.Text;

        for (int i = 0; i < tunes.Length; i++) 
        {
            if (b.Text == tun[i].TuneName)
            {
                Console.Beep(tun[i].Frequency, 200);
                Input.Items.Add(b.Text);
                Output.Items.Add(tun[i].TuneName);
                if (startButtonPressed == true)
                {
                    filename2 = musicFileName + ".csv";

                    File.WriteAllText(filename2, tun[i].TuneName);
                    RecordList.Items.Add(tun[i].TuneName);
                }
            }
        }           
    }

The Error comes at Line : File.WriteAllText()... It says that the File can not be used, because it's used by an another process,but I havent opened any File.

Alex C
  • 16,624
  • 18
  • 66
  • 98
xDropzZ
  • 1
  • 1
  • Is it open in Excel or some other editor? – xxbbcc May 21 '15 at 18:52
  • 1
    Does your musicFileName contain a path? Use the Path class to create the proper path and file name and extension. – LarsTech May 21 '15 at 18:59
  • You haven't defined `musicFileName`; what is it? If it's global then anything in your application could have opened it. – Dour High Arch May 21 '15 at 19:30
  • I have defined musicFileName in a another form and gave the value to the Form1 – xDropzZ May 21 '15 at 19:34
  • In feauture Questions, it would be nice if you explain your variables a bit more, especially if you use self created types and you dont show them. It also would make it easier to understand your code if you remove unnecessary code like the *Console.Beep()* which obviously has nothing to do with your problem. – LuckyLikey May 21 '15 at 19:38

3 Answers3

0

You need to make sure that the variable filename2 contains a valid path like C:\temp\myfile and not just myfile additionally you might need to run visual studio with elevated privilege if the location is not accessible otherwise.

Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52
0

You could also use streamwriter...

using (StreamWriter writer =new StreamWriter(musicFileName + ".csv";))
    {
        writer.Write(tun[i].TuneName);

    }
TGarrett
  • 562
  • 4
  • 15
  • Try making a new file, and ensure you have write permissions to it. Maybe restart your computer to ensure no process is tied into it? – TGarrett May 21 '15 at 19:13
  • Well, with the code you posted, does not seem to be an issue there. Do you open that file somewhere else in your code causing it to be held open. Do you get that error on the first iteration or second? – TGarrett May 21 '15 at 19:19
  • I created the File in a another Form,I'm giving the value of the name to the other Form and then I'm closing the Form were I've created it. – xDropzZ May 21 '15 at 19:26
0

I'd use a Filestream generated by File.Create(), but I'd make the loop inside the using statement, so you ensure that all ressources will be released at the end (that's why you use using).

        using (FileStream fs = File.Create(Path.Combine(musicFileName, ".csv")))
        {
            foreach (tun in tunes)
            {
                fs.Write(tun.TuneName);
            }
        }

The problem you were actually having is, that you were never closing your file. You should look up using-keyword. It can used only with classes implementing the IDisponsable Interface. It then will call disponse() at the end of the using block and all ressources will be released, eg the file will be closed.

LuckyLikey
  • 3,504
  • 1
  • 31
  • 54