Demonstrates reading a file into memory, attaching it to a MemoryStream and using stream methods to access the contents : File MemoryStream « File Stream « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » File Stream » File MemoryStreamScreenshots 
Demonstrates reading a file into memory, attaching it to a MemoryStream and using stream methods to access the contents

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// MemStrm.cs -- Demonstrates reading a file into memory, attaching it to a
//               MemoryStream and using stream methods to access the contents
//
//               Compile this program with the following command line:
//                   C:>csc MemStrm.cs
using System;
using System.IO;

namespace nsStreams
{
    public class MemStrm
    {
        const string USA = "[USA]";
        const string PhoneEntry = "Phone_number=";
        static public void Main ()
        {
            FileStream cfg;
            try
            {
                cfg = new FileStream ("./config.ini",
                                      FileMode.Open,
                                      FileAccess.ReadWrite);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine ("Cannot find ./config.ini");
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine (e.Message);
                Console.WriteLine ("Cannot find ./config.ini");
                return;
            }
            MemoryStream mem = new MemoryStream ((intcfg.Length);
            cfg.Read (mem.GetBuffer()0(intcfg.Length);
            int pos = FindInBuffer (USA, 0, mem.GetBuffer());
            if (pos < 0)
            {
                Console.WriteLine ("Could not find match in buffer");
            }
            else
            {
                pos = FindInBuffer (PhoneEntry, pos, mem.GetBuffer());
                if (pos < 0)
                {
                    Console.WriteLine ("Could not find phone number");
                }
                else
                {
                    const string NewPhone = "1888555-9876";
                    mem.Seek (pos + PhoneEntry.Length, SeekOrigin.Begin);
                    for (int x = 0; x < NewPhone.Length; ++x)
                    {
                          mem.WriteByte ((byteNewPhone[x]);
                    }
                    cfg.SetLength (0);
                    cfg.Write (mem.GetBuffer()0,
                              (intmem.GetBuffer().Length);
                }
            }
            cfg.Flush ();
            cfg.Close ();
            mem.Close ();
        }
//
// Find a string of characters in a buffer of type byte
        static int FindInBuffer (string ToFind, int start, byte [] buf)
        {
            for (int x = start; x < buf.Length; ++x)
            {
                if (buf[x== (byteToFind[0])
                {
                    int y;
                    for (y = 1; y < ToFind.Length; ++y)
                    {
                        if ((x + y>= buf.Length)
                            break;
                        if (buf[x + y!= (byteToFind[y])
                            break;
                    }
                    if (y == ToFind.Length)
                    {
                        return (x);
                    }
                }
            }
            return (-1);
        }
//
// Convert a buffer of type string to byte
        static void StringToByte (out byte [] b, string str)
        {
            b = new byte [str.Length];
            for (int x = 0; x < str.Length; ++x)
            {
                b[x(bytestr [x];
            }
        }
//
// Convert a buffer of type byte to a string
        static string ByteToString (byte [] b, int start)
        {
            string str = "";
            for (int x = start; x < b.Length; ++x)
            {
                str += (char[x];
            }
            return (str);
        }
    }
}

//File: config.ini
/*
[PROGRAM]
Program Directory=C:\TEMP\
Data Directory=

[DEFAULTS]
Phone_number=800-555-2345
Wallpaper=wallppr.bmp
sshow=default

[Modem]
Initialization String=ATX4L1
Dial Type=1

[Countries]
1=USA
2=Canada
3=United Kingdom

[USA]
Phone_number=1800555-1234
TOLLFREE=1

[Canada]
Phone_number=1800555-2345
TOLLFREE=1

[United Kingdom]
Phone_number=08009872345
TOLLFREE=1

*/


           
       
Related examples in the same category
1. Demonstrate MemoryStreamDemonstrate MemoryStream
2. illustrates use of MemoryStreams
w__w_w._j_a__v___a__2_s.__co___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.