I am developing a rich text editor in wpf.its save as function work perfectly but i also want to provide save option which save the already open document with same name at same location like a notepad or word save option...
i am posting my source code here plz help me..
.Xaml file
<RichTextBox Height="420.724" Margin="6,66,6,5.999" Name="txtcreatelssn" Width="642" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" SpellCheck.IsEnabled="True"/>
<ToolBar Height="34" Margin="6,34,6,0" Name="toolBarMenu" VerticalAlignment="Top" >
<Button Click="CmdNew" >
<Image Source="/MTDLL_TEACHER;component/Images/filenew.png" >
</Image>
</Button>
<Button x:Name="Open" Click="Open1" >
<Image Source="/MTDLL_TEACHER;component/Images/fileopen.png" ></Image>
</Button>
<Button x:Name="Save_As" Click="save_as">
<Image Source="/MTDLL_TEACHER;component/Images/filesave.png"></Image>
</Button>
<Button x:Name="Save" Click="save">
<Image Source="/MTDLL_TEACHER;component/Images/filesaveas.png"></Image>
</Button>
<Button Command="Cut" ToolTip="Cut">
<Image Source="/MTDLL_TEACHER;component/Images/editcut.png"></Image>
</Button>
<Button Command="Copy" ToolTip="Copy">
<Image Source="/MTDLL_TEACHER;component/Images/editcopy.png"></Image>
</Button>
<Button Command="Paste" ToolTip="Paste">
<Image Source="/MTDLL_TEACHER;component/Images/editpaste.png"></Image>
</Button>
<Button Command="Undo" ToolTip="Undo">
<Image Source="/MTDLL_TEACHER;component/Images/editundo.png"></Image>
</Button>
<Button Command="Redo" ToolTip="Redo">
<Image Source="/MTDLL_TEACHER;component/Images/editredo.png"></Image>
</Button>
<Button Command="EditingCommands.ToggleBold" ToolTip="Bold">
<TextBlock FontWeight="Bold" FontSize="15" Foreground="Black">B</TextBlock>
</Button>
<Button Command="EditingCommands.ToggleItalic" ToolTip="Italic">
<TextBlock FontStyle="Italic" FontWeight="Bold" FontSize="15" Foreground="Black">I</TextBlock>
</Button>
<Button Command="EditingCommands.ToggleUnderline" ToolTip="Underline">
<TextBlock TextDecorations="Underline" FontWeight="Bold" FontSize="15" Foreground="Black">U</TextBlock>
</Button>
<Button Command="EditingCommands.AlignLeft" ToolTip="Align Left">
<Image Source="/MTDLL_TEACHER;component/Images/paragraphleftjustify.png"/>
</Button>
<Button Command="EditingCommands.AlignCenter" ToolTip="Align Center">
<Image Source="/MTDLL_TEACHER;component/Images/paragraphcenterjustify.png"/>
</Button>
<Button Command="EditingCommands.AlignRight" ToolTip="Align Right">
<Image Source="/MTDLL_TEACHER;component/Images/paragraphrightjustify.png"/>
</Button>
</ToolBar>
.Xaml.cs file
#region wordfunctionality
void CmdNew(object sender, RoutedEventArgs e)
{
txtcreatelssn.Document = new FlowDocument();
}
void Open1(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
openFile.Filter = "RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";
if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
TextRange documentTextRange = new TextRange(txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);
using (FileStream fs = File.Open(openFile.FileName, FileMode.Open))
{
if (System.IO.Path.GetExtension(openFile.FileName).ToLower() == ".rtf")
{
documentTextRange.Load(fs, System.Windows.DataFormats.Rtf);
}
else
{
documentTextRange.Load(fs, System.Windows.DataFormats.Xaml);
}
}
}
}
void save_as(object sender, RoutedEventArgs e)
{
System.Windows.Forms.SaveFileDialog savefile = new System.Windows.Forms.SaveFileDialog();
savefile.Filter = "RichText Files (*.rtf)|*.rtf|All Files (*.*)|*.*";
if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
TextRange documentTextRange = new TextRange(
txtcreatelssn.Document.ContentStart, txtcreatelssn.Document.ContentEnd);
MemoryStream ms = new MemoryStream();
using (FileStream fs = File.Create(savefile.FileName))
{
if (System.IO.Path.GetExtension(savefile.FileName).ToLower() == ".rtf")
{
documentTextRange.Save(fs,System.Windows.DataFormats.Rtf);
}
else
{
documentTextRange.Save(fs, System.Windows.DataFormats.Xaml);
}
}
}
}
#endregion wordfunctionality