microsoft.public.dotnet.languages.csharp - General discussion regarding the C# .NET programming language. including best practices on developing with C# in VS, documentation, setup, and samples.
hey all, is there a way in a console app to download a file (byte array)? thanks, rodchar
In the code below, I open a file using filestream, copy it to a byte array, and the write that array out to a new file. But when I check the size of the original file and the new file, the new file has one less byte than the original file (although the file itself still works fine). I was hoping someone could explain what is going on (why is there a byte missing, and what might that byte be). string originalFile = @"C:\MP3Test\Original.mp3"; string copiedFile = @"C:\MP3Test\Copied.mp3"; // Read file into byte array FileStream fs1 = new FileStream(originalFile, FileMode.OpenOrCreate, FileAccess.Read); byte mp3ByteArray = new byte[fs1.Length]; fs1.Read(mp3ByteArray, 0, System.Convert.ToInt32(fs1.Length)); fs1.Close(); // Write same byte array to new file int arraySize = new int(); arraySize = mp3ByteArray.GetUpperBound(0); FileStream fs2 = new FileStream(copiedFile, FileMode.OpenOrCreate, FileAccess.Write); fs2.Write(mp3ByteArray, 0, arraySize); fs2.Close(); // Compare size of original an new file FileInfo fileInfo1 = new FileInfo(originalFile); FileInfo fileInfo2 = new FileInfo(copiedFile); Console.WriteLine("Size of original file: " + fileInfo1.Length.ToString()); // output====> 20629 bytes Console.WriteLine("Size of copied file: " + fileInfo2.Length.ToString()); // output====> 20628 bytes. One byte less than original file. Thanks, Terry
Background: I have an old program that used to be based on byte alignment, and now must be based on quad word (eight byte) alignment. My problem is with one particular structure that is used to read/write from a disk file, using the structure image. For backwards compatibility, I need it to have one byte alignment (packed) instead of the default eight byte the rest of the project has (i.e., to read existing files). Problem: I know how to use the pragma pack option to force one byte alignment for the structure and code involved. What I'm wondering about is mixing stuff from the rest of the program. For example, if I have an AnsiString declared in the eight byte alignment portion, and I pass it into the one byte alignment code (i.e., as a function argument), does the one byte code potentially have a different layout for the AnsiString data (as AnsiString is an object), thus causing mass confusion? I believe I will be safe passing around integral types (like int, double, char *, etc.), but I'm fuzzy on what happens with objects (like AnsiString or TStringList objects, etc.). I don't believe it's safe to pass structures around, as the eight byte code has it unpacked, but the one byte code would see it as packed (maybe?), but feel free to enlighten me :). Thanks, PaulR