MLyons10

Hi, I have a listBox that a user would enter data in (text as opposed to files, etc.). I would like, when the user clicks the Save button for the application to save line one of text as "1.txt", line two of text as "2.txt", etc. Can this be done

Here is the code that I have been trying to work out:

using (FileStream fs = new FileStream(@"c:\projects\", FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.ASCII))
{
w.WriteLine(listBox1.Text.Item1);
}
}

I've been trying to read up on this, and play around with the code, but I cannot seem to get this to work without getting a lot of errors... Does anyone have any idea what I'm missing, or what I need to do I would greatly appreciate any help and information anyone is able to give me.

Thanks,


Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

jrboddie

You are missing a loop that iterates over all items in your listbox and, of course, you need to create the different filenames.





Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

MLyons10

Thank you for your response, should the code look more like this

string tpath = @"c:\projects\" + textBox19.Text + @"\";
for (int i = 0; i < listBox1.Items.Count; i++)
{
string SourceFile = listBox1.ItemsIdea.ToString();
string DestFile = string.Format("{0}T{1:000}", tpath, i + 1, ".txt");
}

I don't get any errors, but it also doesn't save anything...lol... This code above appears from what I've read to be more geared towards pulling data out of a listBox (As opposed to the code that I posted earlier which seems to be used more for textBoxes)...

Any help and information is greatly appreciated.

Thanks Again,





Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

MLyons10

The lightbulb should be a "[ i ]"... Not sure why I'm getting lightbulbs in all my posts today...lol




Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

jrboddie

See if this works for you:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

int i = 0;

foreach (string item in listBox1.Items)

{

string filename = @"c:\projects\"+i++.ToString()+".txt";

using (StreamWriter writer = new StreamWriter(filename))

{

writer.Write(item);

}

}

}





Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

MLyons10

Thank you very much, where would I specify the file naming format For example, I would like the first file to be named "T001.txt", the second file named "T002.txt", etc. The code for the pictures looks like this:

string DestFile = string.Format("{0}T{1:000}{2}", path, i + 1, Source.Extension);

I highlighted what I don't think is necessary, but don't know where I would put this, or if any other changes would be necessary. The code also does not refer to DestFile, so I don't think VS would recognize that.


Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

jrboddie

In my code change:

int i = 0; to int i = 1;

add:

string path = @"c:\projects\";

change the string filename = @"c:\projects\"+i++.ToString()+".txt";

to

string filename = string.Format("{0}T{1:000}.txt",path, i++);

(I didn't verify this.)






Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

MLyons10

Thank you very much, that works great. I really appreciate your help. I am reading a lot about strings and doing my best to understand it all, but it is a lot to take in. I bought a new book today, so I will start reading that as well.

One final question, if you don't mind. I have one listbox in which I would like to save the files using the same file name that the file has when added to my application, etc. Like, if it is on the users desktop and is called "MyPic01", I would like the application to save it as "MyPic01". How would this be done, would I use FileName

I tried this:

string DestFile = string.Format(FileName);

and

listBox1.Items.FileName

I also tried countless variations on the above based on some articles I had read.

But nothing worked. I've been reading up on this a good bit today, and all the references that I found were about e-mailing the file, which I'm not trying to do.

Any help and information is greatly appreciated.

Thanks Again,





Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

jrboddie

Sorry, I don't understand your question.

By the way, there is nothing magic about 'FileName' or 'filename' in my example. It is just a string which is a collection of characters.





Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

MLyons10

O.K., sorry if I was unclear. Most of the articles I read referenced FileName, so it must just be a common name for a string. I thought that the use of FileName was a reference built into VS to pull the data from the listbox. Sorry if I'm a little confused.

Let me try to be a bit clearer. If I'm not, please let me know.

Presently, in the listBox I have working in the application, it renames the files that it saves from the listBox. I would like a different listBox to save the files without renaming them. In effect I would like to use the same file name as was originally used. I would like to just specify the path.

Also, I was wondering if this can be done with a textbox. For example, if I have a file referenced in a textBox, as the files are referenced in the above listBoxes, can that file be saved as well The textBox does not allow use of the .Items as the listBox does, so would I use a .Copy or something to reference the file When I tried to do this earlier I just kept saving the file path as text, not the actual file. Obviously I made a sizeable error there...lol

Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

jrboddie

Sorry, this is still confusing to me. You are using terms like "saving" and "renaming" and "copying" and "referencing" as if they are all the same thing.

What is in the listboxes: file names full paths

If your second listbox has the original name, and you want to save rename copy the file to the same name, what do you expect to actually happen

Of course a textbox can reference a file.

Look at all of the examples you have now. You should be able to work this out.

Examples (and good code) often use the string 'filename' or 'fileName' or 'FileName' to document that this string contains the name of a file. It could just as easily be 'sam' but it would not be so easily understood.





Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

MLyons10

I'm sorry, I tried to be clear. Let me try again. I would basically like to save the file to the path I designate in code, using the same file name as opposed to renaming it. When I try to change the code to do this I get errors (Or nothing happens at all). I have found some articles and instructions online, but when I try these steps I still get errors. The listBox contains the path and file that the user attaches to the application. Saving would not overwrite the original file, as they would be on different directories. I am able to save the files from the listBox without a problem, but presently it renames the files. I just need to know what to change in the code in order to save the file with the same file name.

As an example, say the user has on their desktop a file called "MyPic01.jpg". They attach this to the application using the listBox. When they click the save button, the file is saved in C:\projects and is called "MyPic01.jpg" as this was the original file name.

I'm sorry, I know that I have received a lot of help on this topic and others, and have in the past adjusted code so that it works for my needs, but every example I have found for saving the file with the original file name throws errors. I don't know what the problem could be. I really feel that I have learned a tremendous amount about programming, but have run into some roadblocks as I try to play around with new controls.

Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

jrboddie

You could use:

System.IO.File.Copy(sourceName, destName);

So what is in the listbox The simple filename with extension like "MyPic01.jpg" or the full path And when you push the button, do you want to do this for all items in the listbox or just the one selected

Try this:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

string source = (string)listBox1.SelectedItem;

string simpleName = Path.GetFileName(source);

string path = @"c:\projects\";

File.Copy(source, path + simpleName);

}

You will need a using System.IO;





Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

MLyons10

Thank you very much, I will play around with this this weekend and will post back. I'm reading up on using strings a lot at the moment, so the code above is very useful as I am trying my best to understand it.

To answer your questions:

1) The listbox could hold either the full path or the file name. I figured that the entire path would be required, so that's how I did it.

2) When the button is clicked I would like it to save all the files in the listBox.

Thanks Again, I greatly appreciate your help and will mark the post as an answer once I get a chance to play around with it.




Re: Visual C# Express Edition Save different lines of text from a listBox to different files?

jrboddie

To save all of the files listed in the box you will need to do it this way:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

string path = @"c:\projects\";

foreach (string source in listBox1.Items)

{

string simpleName = Path.GetFileName(source);

File.Copy(source, path + simpleName);

}

}