-2

I'm trying to develop my first application in C#, and I'm simply trying to read and write from a file. I've done hours of research, had mentors attempt to teach me, but its just not making sense to me.

I've got the beginning of my Form laid out, and what I believe to be the correct syntax for the StreamWriter I'm using, but the file name seems to be used in another process, and I have no idea which. Here's the code I'm working with:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CustomerApplication
{
    public partial class AddCustomerForm : Form
    {
        public AddCustomerForm()
        {
            InitializeComponent();
        }
    private void saveAndExitBtn_Click(object sender, EventArgs e)

    {
        StreamWriter sw = File.AppendText("test.csv");


           sw.WriteLine("Test for Hope");

        // next, retrieve hidden form's memory address
        Form myParentForm = CustomerAppStart.getParentForm();
        // now that we have the address use it to display the parent Form
        myParentForm.Show();
        // Finally close this form
        this.Close();
    }// end saveAndExitBtn_Click method

This is the line that Visual Studio doesn't like :

StreamWriter sw = File.AppendText("test.csv");

Thanks for your time, in advance.

~TT

Jake Tully
  • 13
  • 2
  • 2
    What is the exception? Is `test.csv` in the directory that the executable is in? – Oded Dec 07 '12 at 20:40
  • 1
    +1 Please post exception. Also, what exactly do you mean by `the file name seems to be used in another process, and I have no idea which.` What else is going on? – sǝɯɐſ Dec 07 '12 at 20:50
  • 1
    @JamesEkema -That's probably from the exception message, suggesting that the user is not _closing_ the stream properly. – Oded Dec 07 '12 at 20:51

2 Answers2

2

Educated guess here (based on but the file name seems to be used in another process) - you open the file for writing, but you never close it. So when you later try to write to it again, it fails.

Note that this can also happen if you have the file open in some other application (notepad, for instance).

You should be placing the creation of the stream in a using statement to ensure it gets closed when you finish working with it:

using(StreamWriter sw = File.AppendText("test.csv")
{
    sw.WriteLine("Test for Hope");

    ...
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks everyone for your answers. There is no more errors, but now I'm curious on how to make the user's input into the form write into a file, instead of just my WriteLine code writing information to the file. Then I need to know to read that file in the correct format. This is such a simple task, yet so complex my brain feels like its going to explode! I wish I had all of your experience... – Jake Tully Dec 08 '12 at 00:40
  • @JakeTully - Sounds like you should ask a new question ;) – Oded Dec 08 '12 at 11:33
0

Are you sure you are not holding open the file somewhere else? For example before you step into that line of code you can try and delete the file manually to see if some process is holding onto it. I typed it in and it worked. I would recommend wrapping the Streamwriter in a using statement though and using the FileInfo object. The FileInfo object has a few nice properties on it like you can test to see if the file exists. You can do something like this also.

FileInfo fi = new FileInfo("C:\\test.csv");

if (fi.Exists)
  using (Streamwriter sw = fi.AppendText())
  {
     sw.WriteLine("tst");

     sw.Close();
  }
Rob4md
  • 194
  • 4