gpt4 book ai didi

.net - 如何从 SD 卡读取原始字节?

转载 作者:行者123 更新时间:2023-12-03 14:31:09 24 4
gpt4 key购买 nike

在 Windows PC 上,有没有办法从通过 USB 读卡器连接的 SD 卡读取原始字节? SD 卡是从嵌入式系统写入的,根本不使用文件系统,因此我无法使用标准文件访问例程。这类似于 unix dd 实用程序所做的,但我需要将其集成到 .NET 应用程序中。

最佳答案

我不确定这是否有帮助,但假设即使未格式化的磁盘在 Win32 设备命名空间(我认为必须)中获得物理驱动器编号,您也可以扩展显示的代码 here识别驱动器号并从设备上的任何位置读取原始字节。

更新 : 原链接失效了。来自互联网文件馆的代码。有关上下文,请参阅原始博客文章(上面链接)的 Internet Archive 副本:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.IO;

namespace RawDump
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public enum EMoveMethod : uint
{
Begin = 0,
Current = 1,
End = 2
}

[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint SetFilePointer(
[In] SafeFileHandle hFile,
[In] int lDistanceToMove,
[Out] out int lpDistanceToMoveHigh,
[In] EMoveMethod dwMoveMethod);

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

[DllImport("kernel32", SetLastError = true)]
internal extern static int ReadFile(SafeFileHandle handle, byte[] bytes,
int numBytesToRead, out int numBytesRead, IntPtr overlapped_MustBeZero);

private void buttonDump_Click(object sender, EventArgs e)
{
short FILE_ATTRIBUTE_NORMAL = 0x80;
short INVALID_HANDLE_VALUE = -1;
uint GENERIC_READ = 0x80000000;
uint GENERIC_WRITE = 0x40000000;
uint CREATE_NEW = 1;
uint CREATE_ALWAYS = 2;
uint OPEN_EXISTING = 3;

SaveFileDialog mySFD = new SaveFileDialog();
mySFD.FileName = "dump.bin";
mySFD.InitialDirectory = Path.GetDirectoryName(Application.StartupPath);
if(mySFD.ShowDialog() == DialogResult.OK)
{
SafeFileHandle handleValue = CreateFile(textBoxPath.Text, GENERIC_READ, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (handleValue.IsInvalid)
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

int offset = int.Parse(textBoxOffset.Text, System.Globalization.NumberStyles.HexNumber);
int size = int.Parse(textBoxSize.Text, System.Globalization.NumberStyles.HexNumber);
byte[] buf = new byte[size];
int read = 0;
int moveToHigh;
SetFilePointer(handleValue, offset, out moveToHigh, EMoveMethod.Begin);
ReadFile(handleValue, buf, size, out read, IntPtr.Zero);
FileStream myStream = File.OpenWrite(mySFD.FileName);
myStream.Write(buf, 0, size);
myStream.Flush();
myStream.Close();
handleValue.Close();
}
}
}
}

关于.net - 如何从 SD 卡读取原始字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2598418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com