Best way to read a large file into a byte array in C#?

I have a web server which will read large binary files (several megabytes) into byte arrays. The server could be reading several files at the same time (different page requests), so I am looking for the most optimized way for doing this without taxing the CPU too much. Is the code below good enough? public byte[] FileToByteArray(string fileName) { byte[] buff = null; FileStream fs = ne

在C#中将大文件读入字节数组的最佳方法是什么?

我有一个Web服务器,它会将大的二进制文件(几兆字节)读入字节数组中。 服务器可能同时读取多个文件(不同的页面请求),所以我正在寻找最优化的方式来完成此任务,而不会过多地对CPU进行征税。 代码是否足够好? public byte[] FileToByteArray(string fileName) { byte[] buff = null; FileStream fs = new FileStream(fileName, FileMode.Open,

How to truncate milliseconds off of a .NET DateTime

I'm trying to compare a time stamp from an incoming request to a database stored value. SQL Server of course keeps some precision of milliseconds on the time, and when read into a .NET DateTime, it includes those milliseconds. The incoming request to the system, however, does not offer that precision, so I need to simply drop the milliseconds. I feel like I'm missing something obvious

如何截断.NET DateTime的毫秒数

我试图比较从传入请求到数据库存储值的时间戳。 SQL Server当然会保持一些毫秒的精度,并且在读入.NET DateTime时,它包含那些毫秒。 然而,传入的系统请求并不提供这种精度,所以我需要简单地减少毫秒。 我觉得我错过了一些显而易见的东西,但我还没有找到一种优雅的方式来实现它(C#)。 以下将适用于具有分数毫秒的DateTime,并且还保留Kind属性(Local,Utc或Undefined)。 DateTime dateTime = ... anything ... dat

Can a Byte[] Array be written to a file in C#?

I'm trying to write out a Byte[] array representing a complete file to a file. The original file from the client is sent via TCP and then received by a server. The received stream is read to a byte array and then sent to be processed by this class. This is mainly to ensure that the receiving TCPClient is ready for the next stream and separate the receiving end from the processing end.

是否可以将字节[]数组写入C#文件中?

我试图写出一个表示完整文件的Byte[]数组到一个文件。 来自客户端的原始文件通过TCP发送,然后由服务器接收。 接收到的流被读取到一个字节数组,然后发送给这个类来处理。 这主要是为了确保接收TCPClient已准备好接收下一个流,并将接收端与处理端分开。 FileStream类不会将字节数组作为参数或另一个Stream对象(它允许您将字节写入它)。 我打算通过与原始版本(使用TCPClient的版本)不同的线程完成处理。 我不知道

Extracting bits with a single multiplication

I saw an interesting technique used in an answer to another question, and would like to understand it a little better. We're given an unsigned 64-bit integer, and we are interested in the following bits: 1.......2.......3.......4.......5.......6.......7.......8....... Specifically, we'd like to move them to the top eight positions, like so: 12345678...................................

用一次乘法提取位

我看到一个有趣的技术用于解答另一个问题,并希望更好地理解它。 我们给出了一个无符号的64位整数,我们对以下几点感兴趣: 1.......2.......3.......4.......5.......6.......7.......8....... 具体来说,我们希望将它们移动到前八位,如下所示: 12345678........................................................ 我们不关心由表示的位的值. ,而且他们不必保存。 解决方案是屏蔽不需要的位,并将结果乘以0x2040810

Manipulating bits of a character variable

My objective is to frame a char variable by assigning values to each bit, ie I need to assign 0's and 1's to each bit. I did the following code: char packet; int bit; packet &= ~(1 << 0); packet |= (1 << 1); printf("n Checking each bit of packet: n"); for(int x=0;x<2;x++) { bit = packet & (1 << x); printf("nBit [%d] of packet : %d", x, bit); } B

操作字符变量的位

我的目标是通过为每个位分配值来构造一个char变量,即我需要为每个位分配0和1。 我做了以下代码: char packet; int bit; packet &= ~(1 << 0); packet |= (1 << 1); printf("n Checking each bit of packet: n"); for(int x=0;x<2;x++) { bit = packet & (1 << x); printf("nBit [%d] of packet : %d", x, bit); } 但是我得到的输出是: Bit[0] of packet : 0 Bit[1] of packet :

C question: Padding bits in unsigned integers and bitwise operations (C89)

I have a lot of code that performs bitwise operations on unsigned integers. I wrote my code with the assumption that those operations were on integers of fixed width without any padding bits. For example an array of 32 bit unsigned integers of which all 32 bits available for each integer. I'm looking to make my code more portable and I'm focused on making sure I'm C89 compliant (i

C问题:填充无符号整数和按位运算(C89)

我有很多代码对无符号整数执行按位运算。 我写了我的代码,假设这些操作是在没有任何填充位的情况下是固定宽度的整数。 例如,一个32位无符号整数的数组,其中所有32位可用于每个整数。 我期望让我的代码更具可移植性,我专注于确保我符合C89(在这种情况下)。 我遇到的问题之一是可能的填充整数。 以GMP手册中的这个极端例子为例: 然而,在Cray矢量系统中,可能会注意到short和int总是以8个字节存储(并且sizeof指示

Bitwise operators and "endianness"

Does endianness matter at all with the bitwise operations? Either logical or shifting? I'm working on homework with regard to bitwise operators, and I can not make heads or tails on it, and I think I'm getting quite hung up on the endianess. That is, I'm using a little endian machine (like most are), but does this need to be considered or is it a wasted fact? In case it matters,

按位运算符和“字节顺序”

随着按位操作,排序是否有问题? 无论是逻辑还是移位? 我正在做关于按位运算符的作业,我不能在它上面做正面或反面的事情,而且我觉得我已经挂上了永久性。 也就是说,我正在使用一个小端机器(就像大多数机器一样),但这是需要考虑还是浪费的事实? 如果重要,我使用C. 字节顺序只对内存中的数据布局很重要。 只要处理器加载数据以便操作,字节排序完全无关紧要。 位移,位运算等等的性能与您所期望的相同(逻辑上

Most common C# bitwise operations on enums

For the life of me, I can't remember how to set, delete, toggle or test a bit in a bitfield. Either I'm unsure or I mix them up because I rarely need these. So a "bit-cheat-sheet" would be nice to have. For example: flags = flags | FlagsEnum.Bit4; // Set bit 4. or if ((flags & FlagsEnum.Bit4)) == FlagsEnum.Bit4) // Is there a less verbose way? Can you give examples

对枚举最常见的C#按位操作

在我的生活中,我不记得如何设置,删除,切换或测试位域中的某个位。 要么我不确定,要么我混淆起来,因为我很少需要这些。 所以一个“一点点作弊表”将是很好的。 例如: flags = flags | FlagsEnum.Bit4; // Set bit 4. 要么 if ((flags & FlagsEnum.Bit4)) == FlagsEnum.Bit4) // Is there a less verbose way? 你能给出所有其他常用操作的例子,最好是使用[Flags]枚举的C#语法吗? 我对这些扩展做了更多的工作

line macro: do/while(0) vs scope block

Possible Duplicates: What's the use of do while(0) when we define a macro? Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? do { … } while (0) what is it good for? I've seen some multi-line C macros that are wrapped inside a do/while(0) loop like: #define FOO do { do_stuff_here do_more_stuff } while (0) What are the benefits (

行宏:do / while(0)vs范围块

可能重复: 当我们定义宏时,(0)有什么用处? 为什么在C / C ++宏中有时会出现无意义的do / while和if / else语句? 做{...} while(0)有什么好处? 我见过一些包含在do / while(0)循环中的多行C宏,如下所示: #define FOO do { do_stuff_here do_more_stuff } while (0) 与使用基本块相比,以这种方式编写代码有什么好处(如果有的话): #define FOO { do_stuff_here do_more_

C Bitwise Operators Example

given the following function: int boof(int n) { return n + ~n + 1; } What does this function return? I'm having trouble understanding exactly what is being passed in to it. If I called boof(10), would it convert 10 to base 2, and then do the bitwise operations on the binary number? This was a question I had on a quiz recently, and I think the answer is supposed to be 0, but I'm

C位运算符示例

给出以下功能: int boof(int n) { return n + ~n + 1; } 这个函数返回什么? 我无法准确理解传递给它的内容。 如果我调用boof(10),它是否将10转换为2,然后对二进制数进行按位运算? 最近我有一个问题,我想答案应该是0,但我不知道如何证明。 注意:我知道每个按位运算符是如何工作的,我对输入处理的方式更加困惑。 谢谢! 按位操作不会将数字的底层表示更改为基数2 - 无论如何,CPU上的所有数学运算均使