Jump to content

C# Kinda stupid - but how do I get this to save...

Go to solution Solved by Nuluvius,
15 minutes ago, Alexp10v2 said:

No. I know why it looks different as it's saving the text from the main text box on the program.

All I want it to do is save the one line of text I have in a different text box where on the template, the corresponding line is.

(i.e: in the separate text box is line[1] from the original template. I've edited that line in the text box. Now I want to delete line[1] from the template and insert the "modified line" into the template on the specified line - in this case, line[1]) and then save the original template - ignoring the main text box with everything else in it.

Yes, sure I do understand. I just wanted to make sure that you did as well. It's not really a 'problem' with anything as such, it's a fundamental design issue. Specifically you need to have a think about your design... Try to consider things in terms of representing your data as some kind of model; a set of classes perhaps that you may then work with. Working directly with the text loaded in from the data source text file will only ultimately land you in a complete mess - which is where you're at right now.

 

From the example that you have provided it's obvious that you could map each line to at least one class. This way you have an abstraction to work against and you can represent the data in any way that you wish in your View. You will then be free to save it in either format; CSV or whatever else that you may choose.

Hey everyone.
 

Here is a really simple one for you today - Like I probably know how to do it myself - its just been a long time since I've coded in C#.

 

How do I get a file to save? :P

 

I've got a template text file (a simple .txt) - imported it to the program - did some processing (i.e split the lines, added each new index to an appropriate list) - made some edits (read in the original line I wish to edit in the txt file - made a change - refreshed the lists and the change is there). However, I can not for the life of me figure out how to get it to save the text file.

 

I have been taught it in university - but I just can't seem to get it to work. The dialog appears, but when I chose to save over a file, it gives me the confirmation, but no changes occur.

I've tried saving as a new file - but even then the new file doesn't appear in the directory I've chosen.

Heres my "Broken Save Code"

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = currentDirectoryName;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFileDialog1.FileName;
            }
        }

 

Again, Really stupid, probably really simple.

But any help would be appreciated :P

Ryze of the Phoenix: 
CPU:      AMD Ryzen 5 3600 @ 4.15GHz
Ram:      64GB Corsair Vengeance LPX DDR4 @ 3200Mhz (Samsung B-Die & Nanya Technology)
GPU:      MSI RTX 3060 12GB Aero ITX
Storage: Crucial P3 1TB NVMe Gen 4 SSD, 1TB Crucial MX500, Spinning Rust (7TB Internal, 16TB External - All in-use),
PSU:      Cooler Master MWE Gold 750w V2 PSU (Thanks LTT PSU Tier List)
Cooler:   BeQuite! Prue Rock 2 Black Edition
Case:     ThermalTake Versa J22 TG

Passmark 10 Score: 6096.4         CPU-z Score: 4189 MT         Unigine Valley (DX11 @1080p Ultra): 5145         CryEngine Neon Noir (1080p Ultra): 9579

Audio Setup:                  Scarlett 2i2, AudioTechnica AT2020 XLR, Mackie CR3 Monitors, Sennheiser HD559 headphones, HyperX Cloud II Headset, KZ ES4 IEM (Cyan)

Laptop:                            MacBook Pro 2017 (Intel i5 7360U, 8GB DDR3, 128GB SSD, 2x Thunderbolt 3 Ports - No Touch Bar) Catalina & Boot Camp Win10 Pro

Primary Phone:               Xiaomi Mi 11T Pro 5G 256GB (Snapdragon 888)

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Gungpae said:

What program are you using and did you run it as administrator?

Yes - always run Visual Studio Enterprise as an Admin - just to ensure no compatibility issues.

Ryze of the Phoenix: 
CPU:      AMD Ryzen 5 3600 @ 4.15GHz
Ram:      64GB Corsair Vengeance LPX DDR4 @ 3200Mhz (Samsung B-Die & Nanya Technology)
GPU:      MSI RTX 3060 12GB Aero ITX
Storage: Crucial P3 1TB NVMe Gen 4 SSD, 1TB Crucial MX500, Spinning Rust (7TB Internal, 16TB External - All in-use),
PSU:      Cooler Master MWE Gold 750w V2 PSU (Thanks LTT PSU Tier List)
Cooler:   BeQuite! Prue Rock 2 Black Edition
Case:     ThermalTake Versa J22 TG

Passmark 10 Score: 6096.4         CPU-z Score: 4189 MT         Unigine Valley (DX11 @1080p Ultra): 5145         CryEngine Neon Noir (1080p Ultra): 9579

Audio Setup:                  Scarlett 2i2, AudioTechnica AT2020 XLR, Mackie CR3 Monitors, Sennheiser HD559 headphones, HyperX Cloud II Headset, KZ ES4 IEM (Cyan)

Laptop:                            MacBook Pro 2017 (Intel i5 7360U, 8GB DDR3, 128GB SSD, 2x Thunderbolt 3 Ports - No Touch Bar) Catalina & Boot Camp Win10 Pro

Primary Phone:               Xiaomi Mi 11T Pro 5G 256GB (Snapdragon 888)

Link to comment
Share on other sites

Link to post
Share on other sites

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = currentDirectoryName;
            saveFileDialog1.RestoreDirectory = true ;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFileDialog1.FileName;
            }
        }

Try enabling Restore Directory plus this guy trying the same thing I think:

https://stackoverflow.com/questions/30455297/savefiledialog-initialdirectory

Current Build

Successful Builds

Spoiler
Spoiler

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, Alexp10v2 said:

The dialog appears, but when I chose to save over a file, it gives me the confirmation, but no changes occur.

I've tried saving as a new file - but even then the new file doesn't appear in the directory I've chosen.

Heres my "Broken Save Code"

You have a dialog working, you store the result in your 'fileName' variable... Then what?

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
  fileName = saveFileDialog1.FileName;
  // <-- There's nothing here! What do you do with 'fileName'?
}

What are you doing with 'fileName'?

 

Moreover what technology is this? WinForms or WPF?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Nuluvius said:

WinForms or WPF?

Probably WinForms, judging by the DialogResult return type..  WPF version returns a Nullable<Boolean>.  You can still use the WinForms dialog in WPF...but yeah...


@Alexp10v2, either open a filestream from the filename, or the dialog can open it for you it with SaveFileDialog::OpenFile().

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Yamoto42 said:

Probably WinForms, judging by the DialogResult return type..  WPF version returns a Nullable<Boolean>.  You can still use the WinForms dialog in WPF...but yeah...

Hence the question...

1 minute ago, Yamoto42 said:

either open a filestream from the filename, or the dialog can open it for you it with SaveFileDialog::OpenFile().

That may not be necessary. Based on this description:

14 hours ago, Alexp10v2 said:

I've got a template text file (a simple .txt) - imported it to the program - did some processing (i.e split the lines, added each new index to an appropriate list) - made some edits (read in the original line I wish to edit in the txt file - made a change - refreshed the lists and the change is there). However, I can not for the life of me figure out how to get it to save the text file.

I'd be inclined to consider the File Class, specifically the WriteAllLines and WriteAllText methods.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

41 minutes ago, Nuluvius said:

You have a dialog working, you store the result in your 'fileName' variable... Then what?


if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
  fileName = saveFileDialog1.FileName;
  // <-- There's nothing here! What do you do with 'fileName'?
}

What are you doing with 'fileName'?

 

Moreover what technology is this? WinForms or WPF?

This is a WinForms C# program.

I've managed to get it to save now - but I have another issue.

 

Its saving the file after all the processing (which is good as it shows the save feature is working) - but not what I want. 

I would like it to save the changed line to the original text file, so that when the user re-opens the file, the program will be able to read it. 

Ideally: User specified which line they wish to edit - write out that line to a text box - delete that line from the original file - insert the modifided text in where the line was deleted -save the original file (Not the processed output) - If that makes any sense, its weird to describe.

Ryze of the Phoenix: 
CPU:      AMD Ryzen 5 3600 @ 4.15GHz
Ram:      64GB Corsair Vengeance LPX DDR4 @ 3200Mhz (Samsung B-Die & Nanya Technology)
GPU:      MSI RTX 3060 12GB Aero ITX
Storage: Crucial P3 1TB NVMe Gen 4 SSD, 1TB Crucial MX500, Spinning Rust (7TB Internal, 16TB External - All in-use),
PSU:      Cooler Master MWE Gold 750w V2 PSU (Thanks LTT PSU Tier List)
Cooler:   BeQuite! Prue Rock 2 Black Edition
Case:     ThermalTake Versa J22 TG

Passmark 10 Score: 6096.4         CPU-z Score: 4189 MT         Unigine Valley (DX11 @1080p Ultra): 5145         CryEngine Neon Noir (1080p Ultra): 9579

Audio Setup:                  Scarlett 2i2, AudioTechnica AT2020 XLR, Mackie CR3 Monitors, Sennheiser HD559 headphones, HyperX Cloud II Headset, KZ ES4 IEM (Cyan)

Laptop:                            MacBook Pro 2017 (Intel i5 7360U, 8GB DDR3, 128GB SSD, 2x Thunderbolt 3 Ports - No Touch Bar) Catalina & Boot Camp Win10 Pro

Primary Phone:               Xiaomi Mi 11T Pro 5G 256GB (Snapdragon 888)

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Alexp10v2 said:

Its saving the file after all the processing (which is good as it shows the save feature is working) - but not what I want. 

I would like it to save the changed line to the original text file, so that when the user re-opens the file, the program will be able to read it. 

Ideally: User specified which line they wish to edit - write out that line to a text box - delete that line from the original file - insert the modifided text in where the line was deleted -save the original file (Not the processed output) - If that makes any sense, its weird to describe.

No it's not really clear to be honest. What do you mean by 'processed output'?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Nuluvius said:

No it's not really clear to be honest. What do you mean by 'processed output'?

I thought as much.

 

Here's the template file I have:

Spoiler

template.png.3a71ca21a85c0603ad1ac2c14284a4f1.png

 

The program imports this and does some processing to make it more legible/readable.

However, after making the change I want, it's saving the file with the processing and not overwriting the part I need in the template.

So its saving it like this:

Spoiler

598c617803380_nottemplate.png.f1cedb384ab9aedf4a22678a80c746c7.png

and not like this:

Spoiler

598c618d34318_idealtemplate.png.8c4217cb891cb8a7dceef7beab337a39.png

 

It has to be saved as the format in the 3rd picture - otherwise, when reopened the program cannot understand what goes into the needed lists as its deliminated by a ','

Ryze of the Phoenix: 
CPU:      AMD Ryzen 5 3600 @ 4.15GHz
Ram:      64GB Corsair Vengeance LPX DDR4 @ 3200Mhz (Samsung B-Die & Nanya Technology)
GPU:      MSI RTX 3060 12GB Aero ITX
Storage: Crucial P3 1TB NVMe Gen 4 SSD, 1TB Crucial MX500, Spinning Rust (7TB Internal, 16TB External - All in-use),
PSU:      Cooler Master MWE Gold 750w V2 PSU (Thanks LTT PSU Tier List)
Cooler:   BeQuite! Prue Rock 2 Black Edition
Case:     ThermalTake Versa J22 TG

Passmark 10 Score: 6096.4         CPU-z Score: 4189 MT         Unigine Valley (DX11 @1080p Ultra): 5145         CryEngine Neon Noir (1080p Ultra): 9579

Audio Setup:                  Scarlett 2i2, AudioTechnica AT2020 XLR, Mackie CR3 Monitors, Sennheiser HD559 headphones, HyperX Cloud II Headset, KZ ES4 IEM (Cyan)

Laptop:                            MacBook Pro 2017 (Intel i5 7360U, 8GB DDR3, 128GB SSD, 2x Thunderbolt 3 Ports - No Touch Bar) Catalina & Boot Camp Win10 Pro

Primary Phone:               Xiaomi Mi 11T Pro 5G 256GB (Snapdragon 888)

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Alexp10v2 said:

I thought as much.

 

Here's the template file I have:

-snip-

The program imports this and does some processing to make it more legible/readable.

However, after making the change I want, it's saving the file with the processing and not overwriting the part I need in the template.

So its saving it like this:

-snip-

and not like this:

-snip-

It has to be saved as the format in the 3rd picture - otherwise, when reopened the program cannot understand what goes into the needed lists as its deliminated by a ','

Ok so am I correct in surmising that your issue can be simplified to: You have some data that you load into your application where you fundamentally change it, then you save it back out and you are then confused as to why it looks different?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Nuluvius said:

Ok so am I correct in surmising that your issue can be simplified to: You have some data that you load into your application where you fundamentally change it, then you save it back out and you are then confused as to why it looks different?

No. I know why it looks different as it's saving the text from the main text box on the program.

All I want it to do is save the one line of text I have in a different text box where on the template, the corresponding line is.

(i.e: in the separate text box is line[1] from the original template. I've edited that line in the text box. Now I want to delete line[1] from the template and insert the "modified line" into the template on the specified line - in this case, line[1]) and then save the original template - ignoring the main text box with everything else in it.

Ryze of the Phoenix: 
CPU:      AMD Ryzen 5 3600 @ 4.15GHz
Ram:      64GB Corsair Vengeance LPX DDR4 @ 3200Mhz (Samsung B-Die & Nanya Technology)
GPU:      MSI RTX 3060 12GB Aero ITX
Storage: Crucial P3 1TB NVMe Gen 4 SSD, 1TB Crucial MX500, Spinning Rust (7TB Internal, 16TB External - All in-use),
PSU:      Cooler Master MWE Gold 750w V2 PSU (Thanks LTT PSU Tier List)
Cooler:   BeQuite! Prue Rock 2 Black Edition
Case:     ThermalTake Versa J22 TG

Passmark 10 Score: 6096.4         CPU-z Score: 4189 MT         Unigine Valley (DX11 @1080p Ultra): 5145         CryEngine Neon Noir (1080p Ultra): 9579

Audio Setup:                  Scarlett 2i2, AudioTechnica AT2020 XLR, Mackie CR3 Monitors, Sennheiser HD559 headphones, HyperX Cloud II Headset, KZ ES4 IEM (Cyan)

Laptop:                            MacBook Pro 2017 (Intel i5 7360U, 8GB DDR3, 128GB SSD, 2x Thunderbolt 3 Ports - No Touch Bar) Catalina & Boot Camp Win10 Pro

Primary Phone:               Xiaomi Mi 11T Pro 5G 256GB (Snapdragon 888)

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Nuluvius said:

Ok so am I correct in surmising that your issue can be simplified to: You have some data that you load into your application where you fundamentally change it, then you save it back out and you are then confused as to why it looks different?

Would this work?

 

import the raw template into a hidden text box

Pull the needed line in - make changes - hiddenTextBoxName.Replace(line[1] , editbox.Text);

streamwrite that as false to overwrite the template with the new "modified" data

Ryze of the Phoenix: 
CPU:      AMD Ryzen 5 3600 @ 4.15GHz
Ram:      64GB Corsair Vengeance LPX DDR4 @ 3200Mhz (Samsung B-Die & Nanya Technology)
GPU:      MSI RTX 3060 12GB Aero ITX
Storage: Crucial P3 1TB NVMe Gen 4 SSD, 1TB Crucial MX500, Spinning Rust (7TB Internal, 16TB External - All in-use),
PSU:      Cooler Master MWE Gold 750w V2 PSU (Thanks LTT PSU Tier List)
Cooler:   BeQuite! Prue Rock 2 Black Edition
Case:     ThermalTake Versa J22 TG

Passmark 10 Score: 6096.4         CPU-z Score: 4189 MT         Unigine Valley (DX11 @1080p Ultra): 5145         CryEngine Neon Noir (1080p Ultra): 9579

Audio Setup:                  Scarlett 2i2, AudioTechnica AT2020 XLR, Mackie CR3 Monitors, Sennheiser HD559 headphones, HyperX Cloud II Headset, KZ ES4 IEM (Cyan)

Laptop:                            MacBook Pro 2017 (Intel i5 7360U, 8GB DDR3, 128GB SSD, 2x Thunderbolt 3 Ports - No Touch Bar) Catalina & Boot Camp Win10 Pro

Primary Phone:               Xiaomi Mi 11T Pro 5G 256GB (Snapdragon 888)

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Alexp10v2 said:

No. I know why it looks different as it's saving the text from the main text box on the program.

All I want it to do is save the one line of text I have in a different text box where on the template, the corresponding line is.

(i.e: in the separate text box is line[1] from the original template. I've edited that line in the text box. Now I want to delete line[1] from the template and insert the "modified line" into the template on the specified line - in this case, line[1]) and then save the original template - ignoring the main text box with everything else in it.

Yes, sure I do understand. I just wanted to make sure that you did as well. It's not really a 'problem' with anything as such, it's a fundamental design issue. Specifically you need to have a think about your design... Try to consider things in terms of representing your data as some kind of model; a set of classes perhaps that you may then work with. Working directly with the text loaded in from the data source text file will only ultimately land you in a complete mess - which is where you're at right now.

 

From the example that you have provided it's obvious that you could map each line to at least one class. This way you have an abstraction to work against and you can represent the data in any way that you wish in your View. You will then be free to save it in either format; CSV or whatever else that you may choose.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Nuluvius said:

Yes, sure I do understand. I just wanted to make sure that you did as well. It's not really a 'problem' with anything as such, it's a fundamental design issue. Specifically you need to have a think about your design... Try to consider things in terms of representing your data as some kind of model; a set of classes perhaps that you may then work with. Working directly with the text loaded in from the data source text file will only ultimately land you in a complete mess - which is where you're at right now.

 

From the example that you have provided it's obvious that you could map each line to at least one class. This way you have an abstraction to work against and you can represent the data in any way that you wish in your View. You will then be free to save it in either format; CSV or whatever else that you may choose.

I've managed to get it working somehow - but I'll definitely keep this in mind for my next project. 

Thanks for the help :)

Ryze of the Phoenix: 
CPU:      AMD Ryzen 5 3600 @ 4.15GHz
Ram:      64GB Corsair Vengeance LPX DDR4 @ 3200Mhz (Samsung B-Die & Nanya Technology)
GPU:      MSI RTX 3060 12GB Aero ITX
Storage: Crucial P3 1TB NVMe Gen 4 SSD, 1TB Crucial MX500, Spinning Rust (7TB Internal, 16TB External - All in-use),
PSU:      Cooler Master MWE Gold 750w V2 PSU (Thanks LTT PSU Tier List)
Cooler:   BeQuite! Prue Rock 2 Black Edition
Case:     ThermalTake Versa J22 TG

Passmark 10 Score: 6096.4         CPU-z Score: 4189 MT         Unigine Valley (DX11 @1080p Ultra): 5145         CryEngine Neon Noir (1080p Ultra): 9579

Audio Setup:                  Scarlett 2i2, AudioTechnica AT2020 XLR, Mackie CR3 Monitors, Sennheiser HD559 headphones, HyperX Cloud II Headset, KZ ES4 IEM (Cyan)

Laptop:                            MacBook Pro 2017 (Intel i5 7360U, 8GB DDR3, 128GB SSD, 2x Thunderbolt 3 Ports - No Touch Bar) Catalina & Boot Camp Win10 Pro

Primary Phone:               Xiaomi Mi 11T Pro 5G 256GB (Snapdragon 888)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×