问题描述
手头有个sd卡,想写一个字库文件到sd卡的物理扇区,非文件系统的,主要是加快下位机液晶显示速度!限于能力所限,求c#相关调用方法下面有一些参考资料,主要是想打开一个字库文件,例如AAA.BIN,然后依次读入文件并写到sd卡0磁道1扇区开始的位置(只要不占用fat文件系统的扇区即可)usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingMicrosoft.Win32.SafeHandles;usingSystem.Runtime.InteropServices;namespacewhjcns{classSDUtils{privateconstuintGENERIC_READ=0x80000000;privateconstuintGENERIC_WRITE=0x40000000;privateconstuintFILE_SHARE_READ=0x00000001;privateconstuintFILE_SHARE_WRITE=0x00000002;privateconstuintOPEN_EXISTING=3;[DllImport("kernel32.dll",SetLastError=true)]privatestaticexternSafeFileHandleCreateFileA(stringlpFileName,uintdwDesiredAccess,uintdwShareMode,IntPtrlpSecurityAttributes,uintdwCreationDisposition,uintdwFlagsAndAttributes,IntPtrhTemplateFile);privateSystem.IO.FileStream_DriverStream;privatelong_SectorLength=0;privateSafeFileHandle_DriverHandle;///<summary>///扇区数///</summary>publiclongSectorLength{get{return_SectorLength;}}///<summary>///获取磁盘扇区信息///</summary>///<paramname="DriverName">G:</param>publicSDUtils(stringDriverName){if(DriverName==null&&DriverName.Trim().Length==0)return;_DriverHandle=CreateFileA("\\.\"+DriverName.Trim(),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,IntPtr.Zero,OPEN_EXISTING,0,IntPtr.Zero);_DriverStream=newSystem.IO.FileStream(_DriverHandle,System.IO.FileAccess.ReadWrite);GetSectorCount();}///<summary>///扇区显示转换///</summary>///<paramname="SectorBytes">扇区长度512</param>///<returns>EB5290......55AA</returns>publicstringByte2String(byte[]SectorBytes){if(SectorBytes==null||SectorBytes.Length!=512)return"Contentisempty";StringBuilderReturnText=newStringBuilder();intRowCount=0;for(inti=0;i!=512;i++){ReturnText.Append(SectorBytes[i].ToString("X02")+"");if(RowCount==15){ReturnText.Append("rn");RowCount=-1;}RowCount++;}returnReturnText.ToString();}///<summary>///获取扇区数///</summary>privatevoidGetSectorCount(){if(_DriverStream==null)return;_DriverStream.Position=0;byte[]ReturnByte=newbyte[512];_DriverStream.Read(ReturnByte,0,512);//获取第1扇区if(ReturnByte[0]==0xEB&&ReturnByte[1]==0x58)//DOS的好象都是32位{_SectorLength=(long)BitConverter.ToInt32(newbyte[]{ReturnByte[32],ReturnByte[33],ReturnByte[34],ReturnByte[35]},0);}if(ReturnByte[0]==0xEB&&ReturnByte[1]==0x52)//NTFS好象是64位{_SectorLength=BitConverter.ToInt64(newbyte[]{ReturnByte[40],ReturnByte[41],ReturnByte[42],ReturnByte[43],ReturnByte[44],ReturnByte[45],ReturnByte[46],ReturnByte[47]},0);}}///<summary>///读一个扇区///</summary>///<paramname="SectorIndex">扇区号</param>///<returns>如果扇区数字大于分区信息的扇区数返回NULL</returns>publicbyte[]ReadSector(longSectorIndex){//if(SectorIndex>_SectorLength)//returnnull;_DriverStream.Position=SectorIndex*512;byte[]ReturnByte=newbyte[512];_DriverStream.Read(ReturnByte,0,512);//获取扇区returnReturnByte;}///<summary>///写入数据///</summary>///<paramname="SectorBytes">扇区长度512</param>///<paramname="SectorIndex">扇区位置</param>publicvoidWriteSector(byte[]SectorBytes,longSectorIndex){if(SectorBytes.Length!=512)return;if(SectorIndex>_SectorLength)return;_DriverStream.Position=SectorIndex*512;_DriverStream.Write(SectorBytes,0,512);//写入扇区}///<summary>///关闭///</summary>publicvoidClose(){_DriverStream.Close();}}}
解决方案
解决方案二:
sd卡没有什么磁道柱面之类的概念。只有扇区,编号从0~总容量(字节数)/512-1。和硬盘一样,0扇区存储了分区表,一般默认0x10000扇区开始是第一个分区(你可以用winhex看下)对于fat32来说,分区开始是文件分配表,以及备份的文件分配表。
解决方案三:
该回复于2014-01-13 17:12:35被版主删除
解决方案四:
读写sd扇区多少有些眉目,现在怎么扩大保留扇区呢(增加出来的扇区我要物理写一些数据)?我的2gsd卡格式化完保留扇区是38个,现在怎么把他扩大成例如512个,同时还要将fat1,fat2,根目录区,数据区之类的往后挪?怎么才能实现啊!
解决方案五:
我最近也遇到了这样的问题,请问你的是怎么解决的????