streamreader and writer into same textfile

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 693 Views - Last Post: 15 June 2019 - 10:55 AM Rate Topic: -----

#31 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader and writer into same textfile

Posted 12 June 2019 - 06:42 AM

Well, the code I wrote was actually very short, I even skipped using double for loops to get access to the list array string. However this task were just measuring up to C in just one task while I solved all E taskts which were 3 pieces. I think it is okay so I sent to the teacher already
Was This Post Helpful? 0
  • +
  • -

#32 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,666
  • Joined: 05-May 12

Re: streamreader and writer into same textfile

Posted 12 June 2019 - 06:44 AM

View PostRealme, on 12 June 2019 - 04:00 AM, said:

I tried and the "lines" were empty

If you are talking about lines from this code form post #14:
        public Form1()
        {
            InitializeComponent();
            rng = new Random();
            string[] lines = { richTextBox1.Text };

            :


Then lines cannot be empty. It'll be an array that contains one string element. That string element will contain whatever the Text property of richTextBox1 happens to contain at that moment in time. Without seeing your Form1.Designer.cs code, I can't tell whether Text is an empty string, or if it contains some other string that you set in the Windows Forms Designer. People coming from a VB6 background for some reason like to pre-fill Windows controls in the Designer, but people from C/C++ and Java backgrounds don't for some reason.
Was This Post Helpful? 0
  • +
  • -

#33 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader and writer into same textfile

Posted 12 June 2019 - 07:28 AM

I checked it was very strange. Richtextbox did not get applied to string array lines but when I wrote "messagebox.show(richtextbox) it showed whenever I wrote something in the richtextbox but never got in string array so I just decided to send it without the function. It was too much
Was This Post Helpful? 0
  • +
  • -

#34 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,666
  • Joined: 05-May 12

Re: streamreader and writer into same textfile

Posted 12 June 2019 - 07:53 AM

It's a matter of timing. Is your MessageBox.Show(richTextBox1.Text) call in the constructor? Or is it someplace else in one of your button click handlers?

This construct does not store a reference to richTextBox1.Text within the array:
string [] lines = { richTextBox1.Text };


It will take the current value of richTextBox1.Text at that point in time.

Strings in C# (usually) are treated as value types, not as reference types.
Was This Post Helpful? 0
  • +
  • -

#35 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader and writer into same textfile

Posted 15 June 2019 - 09:51 AM

Hi, I have one last trouble I haven't been able to solve. To read and write to same text file like following:

FileStream outStream = new FileStream(@"C:\Users\razer\source\repos\WindowsFormsApp2\test.txt", FileMode.Append, FileAccess.Write);
                using (StreamWriter writer = new StreamWriter(outStream))
                {
                    writer.Write(richTextBox1.Text);
                }
                outStream.Dispose();


 StreamReader file = new StreamReader(@"C:\Users\razer\source\repos\WindowsFormsApp2\test.txt", Encoding.Default, false);
                // streamreader läser in textdokument


When I run the application it says 'The process cannot access the file 'C:\Users\razer\source\repos\WindowsFormsApp2\test.txt' because it is being used by another process.'


how can I deal with this and make it work?
Was This Post Helpful? 0
  • +
  • -

#36 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,394
  • Joined: 12-June 08

Re: streamreader and writer into same textfile

Posted 15 June 2019 - 10:21 AM

Why would you be using a file stream in your streamwriter? Just use a path.

Again - read the docs and see the options available as a constructor for you.
https://docs.microso...etframework-4.8
Was This Post Helpful? 0
  • +
  • -

#37 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: streamreader and writer into same textfile

Posted 15 June 2019 - 10:55 AM

However I managed to solve it, I did this FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

but I skipped this task anyway, I needed to change a lot in my code to get it to work so I skipped it
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3