API方式 获取Windows下的 Windows目录和 Windows/System32目录

来源:互联网 发布:录音软件没有声音 编辑:程序博客网 时间:2024/05/17 05:19

     //声明API 

        /// <summary>
        /// 获取系统的System32目录
        /// </summary>
        /// <param name="lpBuffer"></param>
        /// <param name="nSize"></param>
        /// <returns></returns>
        [DllImport("kernel32", EntryPoint = "GetSystemDirectoryA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern long GetSystemDirectory(StringBuilder lpBuffer, long nSize);
        /// <summary>
        /// 获取系统的Windows目录
        /// </summary>
        /// <param name="lpBuffer"></param>
        /// <param name="nSize"></param>
        /// <returns></returns>
        [DllImport("kernel32", EntryPoint = "GetWindowsDirectoryA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        public static extern long GetWindowsDirectory(StringBuilder lpBuffer, long nSize);
      
       //调用 
        public string GetSystemPath()
        {
            StringBuilder p = new StringBuilder(100);          
            long length;
            length = GetSystemDirectory(p, 100);           
            return p.ToString();
        }
        public string GetWindowsPath()
        {
            StringBuilder p = new StringBuilder(100);
            long length;
            length = GetWindowsDirectory(p, 100);
            return p.ToString();
        }

原创粉丝点击