How to write / create a text file Visual Studio 2012 for windows 8 App. c#

• Question

• Hey!

I'm trying to create and write something in a text file, via C# Visual Studio 2012 for Windows 8.

How i can do that? Because i tried a lot of methodes.

One of them is

StreamWriter sr = new StreamWriter("C:\\file.txt")StreamWriter sr = new StreamWriter(filename) and moore more and i get this error: Error	1	The best overloaded method match for 'System.IO.StreamWriter.StreamWriter(System.IO.Stream)' has some invalid arguments	C:\Users\Adelin\Desktop\App\Test\Test\MainPage.xaml.cs	64	31	TestError	2	Argument 1: cannot convert from 'string' to 'System.IO.Stream'	C:\Users\Adelin\Desktop\App\Test\Test\MainPage.xaml.cs	64	48	Test
Please HELP !!!
• Moved by Wednesday, June 25, 2014 8:13 PM WinStore related
Wednesday, June 25, 2014 4:16 PM

All replies

• That's odd. StreamWriter has a ctor that takes a string path, so your code should work.

You could try opening a FileStream explicitly, and passing that into the StreamWriter ctor.

Regards,

Nick.

Wednesday, June 25, 2014 4:44 PM
• http://s9.postimg.org/gtcomzvtr/error_vb2012.png

Just see:(

Wednesday, June 25, 2014 5:11 PM
• Did you miss path ?

string file = @"c:\temp\text.txt";

http://msdn.microsoft.com/en-us/library/6ka1wd3w(v=vs.110).aspxx

Wednesday, June 25, 2014 6:41 PM
• I Didn't!,

OMG I really need help! Can you help me? How can i contact you?

:(

Wednesday, June 25, 2014 7:09 PM
• You're creating a Windows store app?  The ctor that accepts a string is not supported in RT apps, see here.  The reason for this is that RT apps do not have full access to the filesystem like regular applications do, hence the ctors that allow a regular path do not work.  Refer to this MSDN link on the accessible file locations.  Ultimately once you find the correct location that you want using the provided RT functions then you'll be able to write to the file.

Michael Taylor
http://msmvps.com/blogs/p3net

Wednesday, June 25, 2014 8:13 PM
• I assume it is a Windows Store app. Jerry has written a nice about reading/writing files in Windows Store apps. You can read it here: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html

Perhaps, it should help.

Mayur Tendulkar | Microsoft MVP - Windows Phone Development http://www.mayurtendulkar.com

Thursday, June 26, 2014 5:34 AM
• Jerry's post is from the developer preview and is a bit out of date.

Take a look at the current documentation on File handling , Managing app data , and Accessing data and files . If you want to skip to the end read Quickstart: Reading and writing files .

As Michael said, your app won't have access to c:\ by default. To write outside of the app's data directories will require user permission through a file picker, library capability, or equivalent.

--Rob

Thursday, June 26, 2014 5:44 AM
• Thank you all !
Thursday, June 26, 2014 10:46 AM
• OMG AT LAST! I DID IT!  THANK YOU MAYUR FOR THAT LINK, THAT HELPED ME A LOT!

HOW CAN I CHANGE THE DIRECTORY WHERE MY FILE WILL CREATE?

Because i want to create it on Desktop on a Folder
Thursday, June 26, 2014 11:09 AM
• You will need to use a folder or file save picker to let the user choose where to save the file. The app doesn't have access to the desktop on its own. Once the user has chosen the folder once you can cache the folder or file so the app doesn't need to ask again.

This is discussed and demonstrated in the links I provided previously. Both concepts are demonstrated in the File picker sample

Thursday, June 26, 2014 12:08 PM