Rohan Ramani

Rohan Ramani

  • NA
  • 786
  • 143.9k

How to open and read .txt file in UWP?

Aug 31 2020 8:00 AM
Hi All,
 
How to open and read .txt file in UWP?
 
I am getting "Access denied" error by using below code.
  1. using (OpenFileDialog openFileDialog = new OpenFileDialog())  
  2. {  
  3. openFileDialog.InitialDirectory = "c:\\";  
  4. openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";  
  5. openFileDialog.FilterIndex = 2;  
  6. openFileDialog.RestoreDirectory = true;  
  7. if (openFileDialog.ShowDialog() == DialogResult.OK)  
  8. {  
  9. //Get the path of specified file  
  10. filePath = openFileDialog.FileName;  
  11. //Read the contents of the file into a stream  
  12. var fileStream = openFileDialog.OpenFile();  
  13. using (StreamReader reader = new StreamReader(fileStream))  
  14. {  
  15. fileContent = reader.ReadToEnd();  
  16. }  
  17. }  
  18. }  
  19. MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);  
Please provide the solution.

Answers (1)