经纬度距离,修改系统时间,读取ini文件
1. 大家都知道地球是椭圆形的,同时使用一个经度和纬度可以定义唯一的位置。下面是现实从两个经纬度计算它们之间的距离
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace EARTH
{
class Latitude
{
// 赤道半径
private const double EARTH_RADIUS = 6378137.0;
// 平均半径
//private const double EARTH_RADIUS = 6371004.0;
// 把角度变换为弧度
private double rad(double d)
{
return d * Math.PI / 180.0;
}
/// <summary>
/// 通过经纬度计算地球两点间的距离
/// </summary>
/// <param name="lat1">A点纬度</param>
/// <param name="lng1">A点经度</param>
/// <param name="lat2">B点纬度</param>
/// <param name="lng2">B点经度</param>
/// <returns>返回两点间的距离,单位公里</returns>
public double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double b = rad(lng1) - rad(lng2);
double d = Math.Acos(Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Cos(b) + Math.Sin(radLat1) * Math.Sin(radLat2));
return d * EARTH_RADIUS; ;
}
}
}
2.修改系统时间
public class ChangeTime
{
[DllImport("Kernel32.dll")]
public static extern bool SetSystemTime(ref SYSTEMTIME time);
[DllImport("Kernel32.dll")]
public static extern bool SetLocalTime(ref SYSTEMTIME time);
[DllImport("Kernel32.dll")]
public static extern void GetSystemTime(ref SYSTEMTIME time);
[DllImport("Kernel32.dll")]
public static extern void GetLocalTime(ref SYSTEMTIME time);
}
//下面是SYSTEMTIME
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EARTH
{
//[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
public void FromDateTime(DateTime dateTime)
{
wYear = (ushort)dateTime.Year;
wMonth = (ushort)dateTime.Month;
wDayOfWeek = (ushort)dateTime.DayOfWeek;
wDay = (ushort)dateTime.Day;
wHour = (ushort)dateTime.Hour;
wMinute = (ushort)dateTime.Minute;
wSecond = (ushort)dateTime.Second;
wMilliseconds = (ushort)dateTime.Millisecond;
}
public DateTime ToDateTime()
{
return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond);
}
}
}
3.大家都知道windows下的配置文件一般都是ini,这从qq的安装文件中就可以看到。当然系统也提供了很好用的api,可以直接读取
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ReadIni
{
class Program
{
/*
* WritePrivateProfileString方法说明:
* 功能:将信息写入ini文件
* 返回值:long,如果为0则表示写入失败,反之成功。
* 参数1(section):写入ini文件的某个小节名称(不区分大小写)。
* 参数2(key):上面section下某个项的键名(不区分大小写)。
* 参数3(val):上面key对应的value
* 参数4(filePath):ini的文件名,包括其路径(example: "c:\config.ini")。
* 如果没有指定路径,仅有文件名,系统会自动在windows目录中查找是否有对应的ini文件,
* 如果没有则会自动在当前应用程序运行的根目录下创建ini文件。
*/
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
/* GetPrivateProfileString方法使用说明:
* 功能:从ini文件中读取相应信息
* 返回值:返回所取信息字符串的字节长度
* 参数1(section):某个小节名(不区分大小写),如果为空,则将在retVal内装载这个ini文件的所有小节列表。
* 参数2(key):欲获取信息的某个键名(不区分大小写),如果为空,则将在retVal内装载指定小节下的所有键列表。
* 参数3(def):当指定信息,未找到时,则返回def,可以为空。
* 参数4(retVal):一个字串缓冲区,所要获取的字符串将被保存在其中,其缓冲区大小至少为size。
* 参数5(size):retVal的缓冲区大小(最大字符数量)。
* 参数6(filePath):指定的ini文件路径,如果没有路径,则在windows目录下查找,如果还是没有则在应用程序目录下查找,再没有,就只能返回def了。
*/
[DllImport("kernel32")]
public static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
static void Main(string[] args)
{
//StringBuilder refvalue = new StringBuilder();
//GetPrivateProfileString("串口端口设置", "输入端口", "", refvalue, 100, "E:\\XMLini相关\\setttings.ini");
//Console.WriteLine(refvalue);
//Console.Read();
}
}
}
浙公网安备 33010602011771号