1
Answer

unable to create/save a file using SaveFileDialog in WPF

I am designing a WPF application that is able to create a new .txt file in a desired location, edit it by inserting contents in a textblock, and then saving it. However I am unable to create a new file and unable to save it in a desired location.

I am using commands like SaveFileDialog, Filestream as below: -

private SaveFileDialog newFileDialog = null;\\Inside public partial class MainWindow : Window
newFileDialog = new SaveFileDialog();\\ Inside public MainWindow() to create an object
newFileDialog.FileOk += newFileDialogFileOk;\\ public MainWindow() to call the function

saveFileDialog.Filter = "All items | *.*";\\ Inside function private void newFile(object sender, RoutedEventArgs e)... These are event driven functions
saveFileDialog.ShowDialog();\\ Inside function private void newFile(object sender, RoutedEventArgs e)... These are event driven functions

/*Inside function private void newFileDialogFileOk*/
string filefullpath = saveFileDialog.FileName;
FileInfo sr = new FileInfo(filefullpath);
if (sr.Exists)
\{
sr.Replace(filefullpath, filefullpath);
}
else
\{
using (FileStream reader = sr.Create())
\{
Byte[] txt = new UTF8Encoding(true).GetBytes("");
reader.Write(txt, 0, txt.Length);

Attachment: FileMenu.zip

Answers (1)

1
Photo of Bryian Tan
NA 9.4k 887.4k 15y
Hello,

You have to subtract one from the count, because 0 to 19 = 20 counts

string[] ItemArray = new string[20];
for (int i = 0; i <= listBox1.SelectedItems.Count - 1; i++)
{
txtmessage.Text = ((DataRowView)listBox1.SelectedItem).Row.ItemArray[i].ToString();
}


Thanks,
Bryian Tan
Accepted
1
Photo of Anish Khan
NA 25 0 15y

Hi Bryian,
Your answer is good...
But  listBox1.SelectedItems.Count is < 5
and also I am geting error..

Thanks

Anish

0
Photo of Bryian Tan
NA 9.4k 887.4k 15y
Anish,

What is the relationship between the listbox1, ItemArray and the DataView?