File Access 
Author Message
 File Access

Hello all,

I am getting very frustrated with the problem i am having. I am eventually
going to write a program that has file i/o, but before i even attempt to
start with the file io, i just want to write a dummy program to test that i
can read/write to a file.

I've tried following an example on a website that shows me how to write (and
read) a file, but the examples dont compile, or they give me some stupid
Security exception error!! Can someone be kind enough to help me out by
giving me an example of how to write a console program that writes a string
(which contains a few lines of text) to a .TXT file, close the file, then
open it again and display it's contents using a Console.WriteLine call
(unless there is a more efficient method of displaying it). PLEASE HELP!?

Thanks,

Ryan d



Mon, 01 Nov 2004 03:20:49 GMT  
 File Access
Here's one way:

using System;
using System.IO;

namespace ConsoleApplication1
{
        /// <summary>
        /// Summary description for Class1.
        /// </summary>
        class Class1
        {
                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main(string[] args)
                {
                        StreamWriter writer = null;
                        StreamReader reader = null;

                        try {
                                writer = new StreamWriter("c:\\temp\\foo.txt");
                                writer.WriteLine("Line 1");
                                writer.WriteLine("Line 2");
                        } catch(Exception ex) {
                                Console.WriteLine(ex.Message);
                        } finally {
                                writer.Close();
                        }

                        try {
                                reader = new StreamReader("c:\\temp\\foo.txt");

                                while (reader.Peek() > -1) {
                                        string s = reader.ReadLine();
                                        Console.WriteLine(s);
                                }
                        } catch(Exception ex) {
                                Console.WriteLine(ex.Message);
                        } finally {
                                reader.Close();
                        }

                }
        }

Quote:
}

> Hello all,

> I am getting very frustrated with the problem i am having. I am eventually
> going to write a program that has file i/o, but before i even attempt to
> start with the file io, i just want to write a dummy program to test that i
> can read/write to a file.

> I've tried following an example on a website that shows me how to write (and
> read) a file, but the examples dont compile, or they give me some stupid
> Security exception error!! Can someone be kind enough to help me out by
> giving me an example of how to write a console program that writes a string
> (which contains a few lines of text) to a .TXT file, close the file, then
> open it again and display it's contents using a Console.WriteLine call
> (unless there is a more efficient method of displaying it). PLEASE HELP!?

> Thanks,

> Ryan d



Mon, 01 Nov 2004 03:58:50 GMT  
 File Access
Hi,

I would like to know if there is a way to find out the
access permissions for a file. I tried using
FileInfo.Attributes property. But you cannot compare it to
FileAttributes.ReadOnly to find out if it is read only as
Attributes is a combination of the archive, compressed,
directory, hidden, offline, read-only, system, and
temporary file attribute flags.

fi.Attributes == FileAttributes.ReadOnly where fi is a
FileInfo object does not work.

Any suggestions ??

Thanks,
Mini



Sun, 07 Nov 2004 04:39:27 GMT  
 File Access
Hello Mini,

You can use the bit operation to compare if the Attributes's ReadOnly is
set to 1. This is a sample code:

if(FileAttributes.ReadOnly.CompareTo(fi.Attributes &
FileAttributes.ReadOnly) == 0)
        MessageBox.Show("Read Only");

Form more information about the Enum bit operation please see:

Value Type Usage Guidelines
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpge...
ml/cpconvaluetypeusageguidelines.asp

I hope it helps.

Best regards,

Lion Shi, MCSE, MCSD
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.  2001 Microsoft Corporation. All rights
reserved.
--------------------
    Content-Class: urn:content-classes:message


    Subject: File Access
    Date: Tue, 21 May 2002 13:39:27 -0700
    Lines: 18

    MIME-Version: 1.0
    Content-Type: text/plain;
        charset="iso-8859-1"
    Content-Transfer-Encoding: 7bit
    X-Newsreader: Microsoft CDO for Windows 2000
    X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
    Thread-Index: AcIBB5mOdXrc27jVSFSJtGqbXrm42Q==
    Newsgroups: microsoft.public.dotnet.languages.CSharp
    Path: cpmsftngxa07
    Xref: cpmsftngxa07 microsoft.public.dotnet.languages.csharp:62789
    NNTP-Posting-Host: TKMSFTNGXA06 10.201.232.165
    X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

    Hi,

    I would like to know if there is a way to find out the
    access permissions for a file. I tried using
    FileInfo.Attributes property. But you cannot compare it to
    FileAttributes.ReadOnly to find out if it is read only as
    Attributes is a combination of the archive, compressed,
    directory, hidden, offline, read-only, system, and
    temporary file attribute flags.

    fi.Attributes == FileAttributes.ReadOnly where fi is a
    FileInfo object does not work.

    Any suggestions ??

    Thanks,
    Mini



Mon, 08 Nov 2004 12:02:54 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help on FILE pointers and file access

2. Random file access for TXT file

3. Please Help! File access problems

4. trapping file access

5. monitoring file access in c#?

6. File Access

7. Non-blocking file access

8. direct file access

9. file access allowing multiple instances

10. file access and fflush

11. file access

12. File Access Information

 

 
Powered by phpBB® Forum Software