Qt发布程序

使用下面的命令查看Hello.exe所依赖的DLL:

windeployqt Hello.exe

如果还有缺少的DLL,可以根据报错提示用 Everything 工具进行查找,然后放进程序目录就行了。

qt quick 的程序要这样发布

-qmldir 后面是qt的qml目录

 windeployqt daolan.exe -qmldir "D:\Qt\Qt5.8.0\5.8\mingw53_32\qml"

VC HttpClient

一个 VC++ 中使用的 HttpClient 类,可以进行HTTP GET和POST,代码如下:

HttpClient.h

#pragma once
#include <string>
#include <vector>
using namespace std;

class CHttpClient
{
public:
	CHttpClient(void);
	~CHttpClient(void);

	BOOL Get(LPCSTR url, string &result);
};

HttpClient.cpp

#include "stdafx.h"
#include "HttpClient.h"
#include <wininet.h>   
#pragma comment(lib, "wininet.lib") 


CHttpClient::CHttpClient(void)
{
}

CHttpClient::~CHttpClient(void)
{
}


BOOL CHttpClient::Get(LPCSTR url, string &result)
{
	result.clear();

	vector<char> v; 
	const CHAR * szUrl = url; 
	CHAR szAgent[] = ""; 
	HINTERNET hInternet1 =  
		InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL); 
	if (NULL == hInternet1) 
	{ 
		InternetCloseHandle(hInternet1); 
		return FALSE; 
	} 
	HINTERNET hInternet2 =  
		InternetOpenUrl(hInternet1,szUrl,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE,NULL); 
	if (NULL == hInternet2) 
	{ 
		InternetCloseHandle(hInternet2); 
		InternetCloseHandle(hInternet1); 
		return FALSE; 
	} 
	DWORD dwMaxDataLength = 500; 
	PBYTE pBuf = (PBYTE)malloc(dwMaxDataLength*sizeof(TCHAR)); 
	if (NULL == pBuf) 
	{ 
		InternetCloseHandle(hInternet2); 
		InternetCloseHandle(hInternet1); 
		return FALSE; 
	} 
	DWORD dwReadDataLength = NULL; 
	BOOL bRet = TRUE; 
	do  
	{ 
		ZeroMemory(pBuf,dwMaxDataLength*sizeof(TCHAR)); 
		bRet = InternetReadFile(hInternet2,pBuf,dwMaxDataLength,&dwReadDataLength); 
		for (DWORD dw = 0;dw < dwReadDataLength;dw++) 
		{ 
			v.push_back(pBuf[dw]); 
		} 
	} while (NULL != dwReadDataLength); 

	vector<char>::iterator i; 
	for(i=v.begin(); i!=v.end(); i++) 
		result += *i;

	return TRUE;
}

ProcessStartInfo 一定要写全路径

ProcessStartInfo 一定要写全路径,不然会出现一些莫明其妙的错误,比如开机自动启动时打不开。

ProcessStartInfo p1 = new ProcessStartInfo($"{Application.StartupPath}\\PD101AHelper.exe", $"{address} \"{windowShowMsg.Trim()}\"  \"{zhShowMsg2 ?? ""}\"");

p1.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(p1);

获取安卓内置外置SD卡根目录

使用下面的代码,获取安卓内置或外置SD卡根目录:

//内置sd卡路径
String sdcardPath = System.getenv("EXTERNAL_STORAGE");
//内置sd卡路径
String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath();

//外置sd卡路径
String extSdcardPath = System.getenv("SECONDARY_STORAGE");

C#执行DOS命令,返回DOS命令的输出

使用下面的代码,可以实现C#执行DOS命令,并返回DOS命令的输出:

 /// <summary> 
/// 执行DOS命令,返回DOS命令的输出 
/// </summary> 
/// <param name="dosCommand">dos命令</param> 
/// <param name="milliseconds">等待命令执行的时间(单位:毫秒), 
/// 如果设定为0,则无限等待</param> 
/// <returns>返回DOS命令的输出</returns> 
public static string Execute(string command, int seconds)
{
	string output = ""; //输出字符串 
	if (command != null && !command.Equals(""))
	{
		Process process = new Process();//创建进程对象 
		ProcessStartInfo startInfo = new ProcessStartInfo();
		startInfo.FileName = "cmd.exe";//设定需要执行的命令 
		startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出 
		startInfo.UseShellExecute = false;//不使用系统外壳程序启动
		startInfo.RedirectStandardInput = false;//不重定向输入 
		startInfo.RedirectStandardOutput = true; //重定向输出 
		startInfo.CreateNoWindow = true;//不创建窗口 
		process.StartInfo = startInfo;
		try
		{
			if (process.Start())//开始进程 
			{
				if (seconds == 0)
				{
					process.WaitForExit();//这里无限等待进程结束 
				}
				else
				{
					process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒 
				}
				output = process.StandardOutput.ReadToEnd();//读取进程的输出 
			}
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.Message);//捕获异常,输出异常信息
		}
		finally
		{
			if (process != null)
				process.Close();
		}
	}
	return output;
}

web.config中配置数据库连接 

asp.net的开发中,数据库连接信息一般都在是web.config中配置的,配置方法如下:

<connectionStrings>
    <add name="conn" connectionString="Data Source=123.206.101.218;Initial Catalog=MyShop;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient" />
</connectionStrings>

调用方法:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
namespace MyShop
{
    class Db
    {
        public static SqlConnection GetConnection()
        {
            var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
            var conn = new SqlConnection(connectionString);
            return conn;
        }
    }
}