Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear friends

I am creating on .txt file like this

C#
FileStream fileStream = new FileStream(@"C:\mayur.txt", FileMode.Create);

file is created

on next line i am trying to initialise one writer

C#
TextWriter sw = new StreamWriter(@"C:\\mayur.txt");

but it is giving error file is being used by another person on this line

when I create file manually and then this code works fine but this i am not creating file in code

How to solve this problem?
Posted
Updated 1-Dec-11 20:13pm
v3

Take a look at the MSDN page for FileStream[^]. It has a good example of how to create and write to a text file using this class.

Good luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Dec-11 2:12am    
This is useful, of course, my 5, but there is a particular problem: at attempt to open already opened file.
Please see my solution.
--SA
Monjurul Habib 2-Dec-11 3:08am    
nice link, my 5.
You should not open the same file twice. By default, you open them with exclusive access, for a good reason. It would be really dangerous if you could have such dual access. There is not situations when you need to open it twice in the same program. If you still want it, stop wanting it — it will solve your problem.

Use just the StreamWriter; you don't need lower-level stream, but you can get underlying stream when you need it. [EDIT] Just use StreamWriter.BaseStream property. [END EDIT]

If you still don't see how to solve your problem with just one stream writer, please explain what do you want to achieve.

—SA
 
Share this answer
 
v2
Comments
Michel [mjbohn] 2-Dec-11 2:28am    
Good explanation. My 5
Is it a wrong (or maybe dangerous) to open file with FileStream and use this in constructor of StreamWriter as I did in my solution?
I use it this way because with filestream I can set FileMode.
Sergey Alexandrovich Kryukov 2-Dec-11 2:37am    
You don't need it. And you can get underlying stream -- please see the update above, in [EDIT].
You don't need to set file mode, because you simply should use different — StreamWriter(String, Boolean) constructor.
--SA
Michel [mjbohn] 2-Dec-11 3:18am    
Improved my knowledge. Thank you :)
Monjurul Habib 2-Dec-11 3:08am    
nice answer, my 5
Sergey Alexandrovich Kryukov 2-Dec-11 9:29am    
Thank you, Monjurul.
--SA
You are opening the same file twice. After openening/creating you have a stream which you can use for your writer.

Try this:

FileStream fs = new FileStream(@"C:\TestFile.txt", FileMode.Create);
TextWriter tw = new StreamWriter(fs);
            
tw.WriteLine("Test");
            
tw.Close();
fs.Close();
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 2-Dec-11 2:39am    
That is correct but redundant. Stream writer will work as "create" by default. My 4.
--SA
Sergey Alexandrovich Kryukov 2-Dec-11 2:40am    
And to append, you just use different constructor: StreamWriter(String, Boolean). That's it.
--SA
mayur csharp G 2-Dec-11 3:00am    
Thanks to all i applied it as you suggested
RaviRanjanKr 2-Dec-11 15:41pm    
My 5+
Michel [mjbohn] 2-Dec-11 17:12pm    
thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900