c# - How can I open a specific text file without using OpenFileDialog? -


i have rich text editor have created in c#. 1 of features trying add templates. not want user have use openfiledialog navigate template , open file. specify filepath myself user has click 1 button in order open template.

currently, trying achieve using following code:

private void formallettertoolstripmenuitem_click(object sender, eventargs e)     {         try         {             filestream filestream = new filestream(@".\templates\tmp1.rtf", filemode.open);             string str;             str = filestream.tostring();             string filecontents = file.readalltext(filepath);             filecontents = filestream.tostring();             try             {                  if (richtextboxprintctrl1.modified == true);                 {                     newfile();                 }                 richtextboxprintctrl1.rtf = filecontents;             }             catch (exception exception)             {                 messagebox.show("there error opening template. " + exception, "error", messageboxbuttons.ok, messageboxicon.error);             }         }         catch (exception exception)         {             messagebox.show("there error opening template. " + exception, "error", messageboxbuttons.ok, messageboxicon.error);         } 

however, whenever try open template, exception follows:

system.argumentsexception: file format not valid.

however, have tried open file using openfiledialog , works fine. assist me in getting working correctly?

you making heavy weather of loading rtf. code read file string never work, @evanmcdonnal explained. did file dialog based code succeeded that? remember file dialog ui generates file name in string. if code file dialog works, work when file dialog replaced hard coded string.

i suspect part of problem using relative path. perhaps working directory not expect be. should specify full path file.

in case, load rtf call loadfile method of control. recommend passing full path file.

richtextboxprintctrl1.loadfile(fullpathtortffile); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -