Link to home
Start Free TrialLog in
Avatar of Dynotoe
Dynotoe

asked on

C# --- Can't save the edits to text file openned in notepad from a form1 button1 click event. Help please.

Hi everyone,

I click a button in a form that opens a file that is selected in a checkedListBox1 in Windows notepad as follows...

private void button6_Click(object sender, System.EventArgs e)
{
      string CustonListFile = (Form1.MYCustomSymbolListsPath + (string)checkedListBox1.SelectedItem);
      System.Diagnostics.Process.Start(CustonListFile);
}

It launches notepad and the text file fine, however the problem is that although I can edit and use the functionality of the notepad and it's contents, notepad does not allow me to save or save as......the file.  It asks me to make sure the path is correct.  It shows me however that the path location is correct, because a save as dialoge shows it is saving to the corrrect folder because I can see the other files and the current open file in there etc...

How can I fix this?

Thank you in advance for your help.

Regards,

Dynotoe
ASKER CERTIFIED SOLUTION
Avatar of cookre
cookre
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dynotoe
Dynotoe

ASKER



Hi cookre,

Thanks for the quick reply.  :)

did you try to save the the notepad file after typing things into it?   hmmmm....

I checked and thee was a different method that didn't close the StreamReader sr.close();     ;(

However maybe you can answer this....

I have a lines of code...

private void CreateNewCustomSymbolListNotePadItem()
{
      string path = (Form1.MYCustomSymbolListsPath + "NewList.txt");

      // Delete the file if it exists.
      if (File.Exists(path))
      {
            MessageBox.Show("File already exists.  Save file with different name before trying again.");
            //File.Delete(path);
      }
      else
      {
            //Create file
            File.Create(path);
                  
            //Write text to file
            //FileStream fs = new FileStream();
                  
            //Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            //fs.Write(info, 0, info.Length);

            //Close FileStream
            //fs.Close();
      }
}

Anyway....2 questions if I may.........I'm new at this   :)

1)  See the messageBox line........how could I create a dialoge for this so the user can "Continue with overwite" or "Cancel" options........?

2) when I Uncomment the line...

FileStream fs = new FilStream();

and try to compile I get an access error what is this and how can I control it?

If you could show me with code I would greatly appreciate it.


Thanks again Cookre!

Best,

Dynotoe



1) There's an overload to MessageBox.Show() that lets you specify buttons:
iMessageBox.Show Method ("Continue with overwrite?","File Exists!,OKCancel)

It returns OK or Cancel.


2) You need to specify a filename, open mode, and access spec.
Avatar of Dynotoe

ASKER

Hey cookre,

That's cool.

How do I hadle in code when the user clicks for OK or Cancel?

Dynotoe
It returns the string "OK" or "Cancel"