新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → C# 读写分区(按扇区) 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 4973 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: C# 读写分区(按扇区) 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 Dot NET,C#,ASP,VB 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 C# 读写分区(按扇区)


    使用方式

    Zgke.DriverLoader T = new Zgke.DriverLoader("C:");

      byte[] Temp = T.ReadSector(0);

    MessageBox.Show(T.GetString(Temp));

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

    namespace Zgke
    {
        /// <summary>
        /// 磁盘扇区
        /// </summary>
        public class DriverLoader
        {
            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 _DirverStream;
            private long _SectorLength = 0;       
            private SafeFileHandle _DirverHandle;

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

            /// <summary>
            /// 获取磁盘扇区信息
            /// </summary>
            /// <param name="DirverName">G:</param>
            public DriverLoader(string DirverName)
            {
                if (DirverName == null && DirverName.Trim().Length == 0) return;
                _DirverHandle = CreateFileA("\\\\.\\" + DirverName.Trim(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

                _DirverStream = new System.IO.FileStream(_DirverHandle, System.IO.FileAccess.ReadWrite);

                GetSectorCount();
            }


            /// <summary>
            /// 扇区显示转换
            /// </summary>
            /// <param name="SectorBytes">扇区 长度512</param>
            /// <returns>EB 52 90 ......55 AA</returns>
            public string GetString(byte[] SectorBytes)
            {
                if (SectorBytes.Length != 512) return "";
                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 (_DirverStream == null) return;
                _DirverStream.Position = 0;

                byte[] ReturnByte = new byte[512];
                _DirverStream.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;
                _DirverStream.Position = SectorIndex * 512;           
                byte[] ReturnByte = new byte[512];
                _DirverStream.Read(ReturnByte, 0, 512); //获取扇区
                return ReturnByte;
            }
            /// <summary>
            /// 写入数据
            /// </summary>
            /// <param name="SectorBytes">扇区 长度512</param>
            /// <param name="SectorIndex">扇区位置</param>
            public void WritSector(byte[] SectorBytes, long SectorIndex)
            {
                if (SectorBytes.Length != 512) return;
                if (SectorIndex > _SectorLength) return;
                _DirverStream.Position = SectorIndex * 512;
                _DirverStream.Write(SectorBytes, 0, 512); //写入扇区  
            }

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

        }

    }


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2012/7/24 10:09:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/27 2:23:34

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    77.148ms