shan
shan

Reputation: 1371

save file without using save file dialog

I am working on this project still and I am running into a problem. Well here is what I need to do.

When the user clicks the “Save” button, write the selected record to the file specified in txtFilePath (absolute path not relative) without truncating the values currently inside and handle any exceptions that arise.

Ok here is my code:

 private void Save_Click(object sender, EventArgs e)
    {

        string filePath = txtFilePath.Text;

        if (!File.Exists(filePath))
        {
            FileStream fs = File.Create(filePath);
            fs.Close();
        }

        using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
        {
            using (StreamWriter sw = new StreamWriter(fs))
            {
                foreach (string line in employeeList.Items)
                {
                    sw.WriteLine(line);
                }
            }
        }
            }

Now when I go onto my program and want to save something from the employeelist.text that its not being saved to the place I am saving it at. I don;t know if I am missing something in my code or what but it will not save. Here is an example:

I add a person name to this list in employeelist and in the textbox I have a file called C:\employess\employeelist.txt I want to save it to. I click the save button then I go to that employeelist and it is not being saved.

I don't know what I am doing wrong I have been looking online for a solution but I haven't found anything yet. Thanks

Upvotes: 3

Views: 10881

Answers (4)

victorvartan
victorvartan

Reputation: 1002

Starting with .Net Framework 4 you can do it like this:

private void Save_Click(object sender, EventArgs e)
{
        File.AppendAllLines(txtFilePath.Text, employeeList.Items);
}

Of course, you probably would want to add a check to have a valid path and a valid enumeration of strings.

Upvotes: 0

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107508

Some things to double-check:

  • Make sure you don't have the employeelist.txt file open when you're testing
  • Make sure you don't have invalid characters in your file name
  • Make sure your application has permission to save the file to the location you specified
  • Use the debugger to step-through your code and look for swallowed exceptions -- there must be a reason the file is not created.
  • Check that your Save_Click event is wired up to your button -- is the code in your example even running?

Once you check those things, you may want to follow this example for the create vs. append requirement of your project:

string path = txtFilePath.Text;

// This text is added only once to the file.
if (!File.Exists(path)) 
{
    using (StreamWriter sw = File.CreateText(path)) 
    {
        foreach (var line in employeeList.Items)
            sw.WriteLine(line.ToString());
    }   
} 
else 
{
    using (StreamWriter sw = File.AppendText(path)) 
    {
        foreach (var line in employeeList.Items)
            sw.WriteLine(line.ToString());
    }
}

This will create the file if it doesn't exist, or append to it if it does.

Upvotes: 3

Rudu
Rudu

Reputation: 15892

Checking that the file exists and then creating it is a bit unnecessary as this can all be handled by the StreamWriter/FileStream parts. So your above function can be simplified into the following:

public void Save_Click(object sender, EventArgs e)
{
    StreamWriter file = 
      new StreamWriter(txtFilePath.Text, true);//Open and append
    foreach (object item in employeeList.Items) {
       file.WriteLine(item.toString());
    }
    file.Close();
}

[Updated]

What are the types of txtFilePath and employeeList the former suggests it's a text box, the later suggests it's bound to a non-GUI element perhaps? (WAG)

You might also want to append a blank line at the end so that on further saves you can tell it was an append rather than one long list (depending on your needs of course)

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

If the path looks like a relative one (i.e. doesn't begin with a drive letter), then it will be interpreted that way.

  • If you put a full path in the text box, does the file get saved in the proper place? If so, perhaps this is what was intended.
  • If the user doesn't put in a full path, do you have a way to make it one (for example, just sticking C:\ at the beginning)? Or at least can you tell when there isn't a full path, and reject the request?

Upvotes: -1

Related Questions