473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

accessing part of file

Bob
Hi,
I have a newbie question. Lets say I have a very large file and a key
that says the information I am interested in is from byte 1,000,000 to
byte 1,100,000 in the file. How can I read in the bytes of interest
without reading in the entire file?

Thanks,
Bob

Jan 23 '07 #1
3 1088
Hi Bob,

Open a Stream and set the position to 1,000,000, then simply Stream.Read
100,000 bytes. Something along the line of

byte[] data = new byte[100000];
using(FileStream fs = new FileStream("MyFile", FileMode.Open))
{
fs.Position = 1000000;
fs.Read(data, 0, data.Length);
}
On Tue, 23 Jan 2007 07:40:50 +0100, Bob <bs********@yahoo.comwrote:
Hi,
I have a newbie question. Lets say I have a very large file and a key
that says the information I am interested in is from byte 1,000,000 to
byte 1,100,000 in the file. How can I read in the bytes of interest
without reading in the entire file?

Thanks,
Bob


--
Happy Coding!
Morten Wennevik [C# MVP]
Jan 23 '07 #2
Morten Wennevik [C# MVP] wrote:
Open a Stream and set the position to 1,000,000, then simply Stream.Read
100,000 bytes. Something along the line of

byte[] data = new byte[100000];
using(FileStream fs = new FileStream("MyFile", FileMode.Open))
{
fs.Position = 1000000;
fs.Read(data, 0, data.Length);
}
Remembering, of course, to check the return value of Read in case it
didn't manage to read as much data as you asked for :)

Jon

Jan 23 '07 #3
Bob
Beautiful, Thanks so much!

On Jan 23, 2:21 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
Morten Wennevik [C# MVP] wrote:
Open a Stream and set the position to 1,000,000, then simply Stream.Read
100,000 bytes. Something along the line of
byte[] data = new byte[100000];
using(FileStream fs = new FileStream("MyFile", FileMode.Open))
{
fs.Position = 1000000;
fs.Read(data, 0, data.Length);
}Remembering, of course, to check the return value of Read in case it
didn't manage to read as much data as you asked for :)

Jon
Jan 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Lamberti Fabrizio | last post by:
Hi all, I've to access to a network file from an asp pages. I've red a lot of things on old posts and on Microsoft article but I can't still solve my problem. I've got two server inside the...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
5
by: Niloday | last post by:
Hi All, I am trying to access a mapped network drive from a service that I have created. The service needs to create/delete folders/files on a network drive. When I tried to connect to a...
3
by: Arno | last post by:
Hi, I'm using TcpClient for communication between two PC running a small piece of software. The protocol used has been designed internally and is HTTP similar (command line, headers, body). A...
2
by: Mike M | last post by:
Hello, I created a simple C# library assembly that exposes general facilities I would like to use in my ASP.NET web applications. If I added the assembly into the bin directory of my asp.net...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
3
by: psathishreddy | last post by:
Hi I need to access a file on a remote server through ftp. I have to then reformat the file and write it to a different server. I tried searching throught the net ... found a few third party...
0
by: brian | last post by:
I am using the System.IO.Directory.GetDirectories class to do some file searching work. I am trying to access a networked drive from my local computer. The networked drive is actually located on...
1
by: Usenet User | last post by:
From a .NET 1.1 app: need to access a file share on a remote server by its UNC path, i.e., \\server\folder\subfolder\, using specific username and password. The problem is that the credentials...
3
by: djsuson | last post by:
I'm trying to set up an inheritance tree that also uses templates. This is an attempt to simplify some previous code that was filled with redundancies. I'm working with g++ 3.4.6. The code is split...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.