搜索
bottom↓
回复: 6

求一个c# 读写没有文件系统读写sd卡(例如字库)的工具

[复制链接]

出0入0汤圆

发表于 2014-1-13 10:17:42 | 显示全部楼层 |阅读模式
  如题!
最好可以自定义字库的排列顺序,例如正序还是逆序,横排还是纵排!

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2014-1-13 10:55:00 | 显示全部楼层
winhex可以直接读写SD卡,什么C#、正序逆序之类的就不清楚了

出10入61汤圆

发表于 2014-1-13 10:55:50 | 显示全部楼层
源代码来源于网路,感谢共享。稍作修改,去掉了对文件系统的判定。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;

namespace whjcns
{
class SDUtils
{
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;

private const uint FILE_SHARE_READ = 0x00000001;
private const uint FILE_SHARE_WRITE = 0x00000002;

private const uint OPEN_EXISTING = 3;         

[DllImport("kernel32.dll", SetLastError = true)]
private static extern SafeFileHandle CreateFileA(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);


private System.IO.FileStream _DriverStream;
private long _SectorLength = 0;      
private SafeFileHandle _DriverHandle;

/// <summary>
/// 扇区数
/// </summary>
public long SectorLength { get { return _SectorLength; } }


/// <summary>
/// 获取磁盘扇区信息
/// </summary>
/// <param name="DriverName">G:</param>
public SDUtils(string DriverName)
{
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 = new System.IO.FileStream(_DriverHandle, System.IO.FileAccess.ReadWrite);

GetSectorCount();
}


/// <summary>
/// 扇区显示转换
/// </summary>
/// <param name="SectorBytes">扇区长度512</param>
/// <returns>EB 52 90 ......55 AA</returns>
public string Byte2String(byte[] SectorBytes)
{
if (SectorBytes==null || SectorBytes.Length != 512) return "Content is empty";
StringBuilder ReturnText = new StringBuilder();

int RowCount = 0;
for (int i = 0; i != 512; i++)
{
ReturnText.Append(SectorBytes[i].ToString("X02") + " ");

if (RowCount == 15)
{
ReturnText.Append("\r\n");
RowCount = -1;
}

RowCount++;
}

return ReturnText.ToString();

}              
/// <summary>
/// 获取扇区数
/// </summary>
private void GetSectorCount()
{
if (_DriverStream == null) return;
_DriverStream.Position = 0;

byte[] ReturnByte = new byte[512];
_DriverStream.Read(ReturnByte, 0, 512); //获取第1扇区

if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x58)           //DOS的好象都是32位
{
_SectorLength = (long)BitConverter.ToInt32(new byte[] { ReturnByte[32], ReturnByte[33], ReturnByte[34], ReturnByte[35] }, 0);
}
if (ReturnByte[0] == 0xEB && ReturnByte[1] == 0x52)          //NTFS好象是64位
{
_SectorLength = BitConverter.ToInt64(new byte[] { ReturnByte[40], ReturnByte[41], ReturnByte[42], ReturnByte[43], ReturnByte[44], ReturnByte[45], ReturnByte[46], ReturnByte[47] }, 0);
}

}      


/// <summary>
/// 读一个扇区
/// </summary>
/// <param name="SectorIndex">扇区号</param>
/// <returns>如果扇区数字大于分区信息的扇区数返回NULL</returns>
public byte[] ReadSector(long SectorIndex)
{         
//if (SectorIndex > _SectorLength)
//   return null;
_DriverStream.Position = SectorIndex * 512;         
byte[] ReturnByte = new byte[512];
_DriverStream.Read(ReturnByte, 0, 512); //获取扇区
return ReturnByte;
}
/// <summary>
/// 写入数据
/// </summary>
/// <param name="SectorBytes">扇区长度512</param>
/// <param name="SectorIndex">扇区位置</param>
public void WriteSector(byte[] SectorBytes, long SectorIndex)
{
if (SectorBytes.Length != 512) return;
if (SectorIndex > _SectorLength) return;
_DriverStream.Position = SectorIndex * 512;
_DriverStream.Write(SectorBytes, 0, 512); //写入扇区
}


/// <summary>
/// 关闭
/// </summary>
public void Close()
{
_DriverStream.Close();
}   
}
}

出0入0汤圆

 楼主| 发表于 2014-1-13 16:14:16 | 显示全部楼层
本帖最后由 embeddev_1 于 2014-1-13 16:35 编辑
tcm123 发表于 2014-1-13 10:55
源代码来源于网路,感谢共享。稍作修改,去掉了对文件系统的判定。

using System;


谢谢楼上几位,就是3楼这个意思, 这些资料我也检索到了,限于能力有限,想找个再完整些的,例如怎么调用(打开一个字库文件a.bin,依次读入字库内容并写到SD卡物理扇区0磁道1扇区开始的位置(只要不占用fat扇区任意空闲扇区即可,非文件系统方式))?

出0入0汤圆

发表于 2014-1-13 16:25:31 | 显示全部楼层
沙发                     

出0入0汤圆

 楼主| 发表于 2014-1-14 13:55:50 | 显示全部楼层
本帖最后由 embeddev_1 于 2014-1-14 16:06 编辑

读写基本有些思路了!调整保留扇区的工具也找到了,现在就是没有编程实现调整保留扇区的思路?

出0入0汤圆

发表于 2014-1-16 10:10:50 | 显示全部楼层
这样挺折腾的,最好是带文件系统吧
FatFS znFat
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-26 01:20

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表