Solved

files c#

Posted on 2012-04-10
2
159 Views
Last Modified: 2012-04-18
File is not the write context in c# what would be the syntax
 file.Open(1, Server.MapPath("\DBP.txt"), OpenMode.Input, OpenAccess.Read, OpenShare.Shared);

 File.close(1);
0
Comment
Question by:sevensnake77
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 210 total points
Comment Utility
Even in VB, FileOpen is not the recommended way to work. You will have to learn to work with streams.

There are many streams in .NET (FileStream, StreamReader, StreamWriter, StringWriter, StringReader, binary streams, XML streams), you might need to find the one that suits you the best. And in some situations, they need to be used in combination.

The one that is used most often to read text files is the StreamReader:

VB
Dim yourFile As New IO.StreamReader("\DBP.txt")
Dim line As String
line = yourFile.ReadLine

Open in new window

C#
System.IO.StreamReader yourFile = new System.IO.StreamReader("\\DBP.txt");
string line;
line = yourFile.ReadLine();

Open in new window

No need to pass the other parameters, the role of the StreamReader being to read files, it opens by default with those conditions. That is why .NET has many streams, each one is specialized, so they are easier to open and to work with than the old ways.

ReadLine is one of the many methods you can use to read with a StreamReader, so explorer the other ones. Note that the norion of EOF (EndOfFile) has been replaced by the EndOfStream property of the Stream.

And also be aware than in C#, the backslash has a special meaning, so you need to use a double one where you used only one in VB.
0
 
LVL 9

Author Closing Comment

by:sevensnake77
Comment Utility
tks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now