在日常的工作中,有時(shí)候需要將部分信息保存到本地,也就是寫(xiě)入到INI文件中,便于下次使用;在Qt中可以使用QSetting來(lái)進(jìn)行INI文件的讀寫(xiě)。
(1).void QSettings::beginGroup(const QString & prefix)
相當(dāng)于在INI文件中添加了一個(gè)索引,根據(jù)索引的不同,可以讀取相對(duì)應(yīng)的值
(2).void QSettings::endGroup()
將組重置為相應(yīng)的eginGroup()調(diào)用之前的狀態(tài)。
(3).void QSettings::setValue(const QString & key, const QVariant & value)
向INI文件中寫(xiě)入信息,信息以鍵值對(duì)的方式寫(xiě)入到INI文件中,如果key值已經(jīng)存在了,那么key對(duì)應(yīng)的value值便會(huì)被新值覆蓋掉。
(4).QVariant QSettings::value(const QString & key, const QVariant & defaultValue=QVariant()) const
從INI文件中讀取信息,返回設(shè)置鍵的值,如果設(shè)置的鍵不存在,則返回defaultValue,如果未指定默認(rèn)的值,則返回默認(rèn)QVariant()。
(5).QString QSettings::group() const
返回當(dāng)前的組名
Qt開(kāi)發(fā)必備技術(shù)棧學(xué)習(xí)路線和資料
.NET 中,有多種配置文件格式和讀取機(jī)制可用于管理應(yīng)用程序的設(shè)置和配置信息。這包括JSON、XML、INI格式的配置文件,以及環(huán)境變量。下面我們將分別探討如何輕松讀取這些配置文件和環(huán)境變量。
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,易于人閱讀和編寫(xiě),同時(shí)也易于機(jī)器解析和生成。在.NET 中,可以使用 System.Text.Json 命名空間下的類來(lái)讀取 JSON 配置文件。
例如,假設(shè)你有一個(gè)名為 appsettings.json 的配置文件,內(nèi)容如下:
json{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
你可以使用以下代碼讀取該配置文件:
csharpusing System;
using System.IO;
using System.Text.Json;
public class AppSettings
{
public ConnectionStrings ConnectionStrings { get; set; }
public Logging Logging { get; set; }
}
public class ConnectionStrings
{
public string DefaultConnection { get; set; }
}
public class Logging
{
public LogLevel LogLevel { get; set; }
}
public class LogLevel
{
public string Default { get; set; }
public string Microsoft { get; set; }
public string MicrosoftHostingLifetime { get; set; }
}
class Program
{
static void Main()
{
string jsonFilePath="appsettings.json";
string jsonString=File.ReadAllText(jsonFilePath);
var appSettings=JsonSerializer.Deserialize<AppSettings>(jsonString);
Console.WriteLine(appSettings.ConnectionStrings.DefaultConnection);
}
}
XML(Extensible Markup Language)是一種標(biāo)記語(yǔ)言,可以用來(lái)創(chuàng)建自定義的標(biāo)記來(lái)描述存儲(chǔ)的數(shù)據(jù)。在.NET 中,可以使用 System.Xml 命名空間下的類來(lái)讀取 XML 配置文件。
例如,假設(shè)你有一個(gè)名為 app.config 的 XML 配置文件,內(nèi)容如下:
xml<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="Setting1" value="Value1" />
<add key="Setting2" value="Value2" />
</appSettings>
你可以使用以下代碼讀取該配置文件:
csharpusing System;
using System.Configuration;
using System.Xml;
class Program
{
static void Main()
{
var setting1=ConfigurationManager.AppSettings["Setting1"];
var setting2=ConfigurationManager.AppSettings["Setting2"];
Console.WriteLine(setting1);
Console.WriteLine(setting2);
}
}
請(qǐng)注意,XML 配置文件通常需要添加在項(xiàng)目中的 App.config 或 Web.config 文件中,并且需要引用 System.Configuration 程序集。
htaccess文件是運(yùn)行Apache Web Server的Web服務(wù)器的配置文件,對(duì)配置和重定向Apache Web Server文件系統(tǒng)很有用。
請(qǐng)記住.htaccess文件將采用隱藏格式。沒(méi)有人可以通過(guò)URL直接看到它。
.htaccess文件有很多用途。在這里,我將討論創(chuàng)建它們及其用途。
打開(kāi)任何文本編輯器并使用名稱保存文件.htaccess。mod_rewrite在Apache Web服務(wù)器配置中的php.ini文件中啟用擴(kuò)展。
1.禁用目錄列表
如果要禁用文件夾文件列表,請(qǐng)包括以下代碼。
#禁用目錄列表
Options All –Indexes
2.錯(cuò)誤頁(yè)面
您可以為特定錯(cuò)誤設(shè)置錯(cuò)誤頁(yè)面。
示例:
errorDocument 400 http://www.yourdomain.com/error.html
errorDocument 401 http://www.yourdomain.com/error.html
errorDocument 404 http://www.yourdomain.com/error.html
errorDocument 500 http://www.yourdomain.com/error.html
如果要在Apache服務(wù)器中啟用“重寫(xiě)規(guī)則”,則必須編寫(xiě):RewriteEngine on
如果要關(guān)閉此規(guī)則,請(qǐng)將值更改為關(guān)閉。
RewriteEngine on
3.域重定向
要將yourdomain.com永久重定向到www.yourdomain.com,請(qǐng)?zhí)砑右韵麓a:
RewriteCond%{HTTP_HOST} ^ yourdomain.com
RewriteRule(。*)http://www.yourdomain.com / $ 1 [R=301,L]
4.子域重定向
您還可以執(zhí)行子文件重定向映射到該文件夾。這里www.yourdomain.com正在連接到該 website_folder 文件夾。
RewriteCond%{HTTP_HOST} ^ www \ .yourdomain \ .com $
RewriteCond%{REQUEST_URI}!^ / website_folder /
RewriteRule(。*)/ website_folder / $ 1
這里subdomain.yourdomain.com正在連接到該 subdomain_folder 文件夾。
RewriteCond%{HTTP_HOST} ^ subdomain \ .yourdomain \ .com $
RewriteCond%{REQUEST_URI}!^ / subdomain_folder /
RewriteRule(。*)/ subdomain_folder / $ 1
5.舊域重定向
用于將舊域(old.com)重定向到新域(new.com)的.htaccess代碼。
RewriteCond%{HTTP_HOST} ^ old.com
RewriteRule(。*)http://www.new.com/ [R=301,L]
RewriteCond%{HTTP_HOST} ^ www \ .old \ .com
RewriteRule(。*)http://www.new.com/ [R=301,L]
6.友好的URL
友好和信息豐富的URL有助于搜索引擎排名。URL是搜索引擎優(yōu)化過(guò)程中非常重要的一部分。
配置文件參數(shù)[ a-zA-Z0-9_- ]會(huì)打開(kāi)此輸入(有關(guān)更多幫助,請(qǐng)閱讀正則表達(dá)式):http://gurutechnolabs.com/profile.php?username=test成http:// gurutechnolabs.com/test
RewriteRule ^([a-zA-Z0-9 _-] +)$ profile.php?username=$ 1
RewriteRule ^([a-zA-Z0-9 _-] +)/ $ profile.php?username=$ 1
http://gurutechnolabs.com /friends.php?username=test到http://gurutechnolabs.com/friends/test
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)$ friends.php?username=$ 1
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/ $ friends.php?username=$ 1
這里第一個(gè)參數(shù)允許[a-zA-Z0-9_-],第二個(gè)參數(shù)只允許數(shù)字[ 0-9 ]
http://gurutechnolabs.com/friends.php?username=test&page=2至http://gurutechnolabs.com/friends/test/2
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/([0-9] +)$ friends.php?username=$ 1&page=$ 2
RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/([0-9] +)/ $ friends.php?username=$ 1&page=$ 2
7.隱藏文件擴(kuò)展名
假設(shè)您不想顯示網(wǎng)頁(yè)擴(kuò)展名。
示例:
http://www.yourdomain.com/index.html至 http://www.yourdomain.com/index只需添加以下代碼:
RewriteRule ^([^ /。] +)/?$ $ 1.html
1.啟用壓縮:
壓縮對(duì)于減小網(wǎng)頁(yè)大小非常有用。
有兩種壓縮選項(xiàng):
要啟用Deflate模式,請(qǐng)?jiān)?htaccess中添加以下代碼
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
</ifmodule>
啟用GZIP壓縮模式
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
2.添加過(guò)期標(biāo)題:?jiǎn)⒂脼g覽器緩存
Expire標(biāo)頭用于緩存來(lái)自瀏覽器的數(shù)據(jù)。加速網(wǎng)頁(yè)是有幫助的,因?yàn)榫W(wǎng)頁(yè)可以從瀏覽器中檢索數(shù)據(jù),因此無(wú)需從減少HTTP請(qǐng)求的服務(wù)器獲取數(shù)據(jù)。
<IfModule mod_expires.c>
ExpiresActive on
#如果要設(shè)置默認(rèn)過(guò)期日期
ExpiresDefault“訪問(wèn)加1個(gè)月”
#對(duì)于html文檔
ExpiresByType text / html“access plus 0 seconds”
#數(shù)據(jù)
ExpiresByType text / xml“access plus 0 seconds”
ExpiresByType application / xml“access plus 0 seconds”
ExpiresByType應(yīng)用程序/ json“訪問(wèn)加0秒”
# RSS訂閱
ExpiresByType應(yīng)用程序/ rss + xml“訪問(wèn)加1小時(shí)”
#Favicon(無(wú)法重命名)
ExpiresByType image / x-icon“訪問(wèn)加1周”
#Media:圖像,視頻,音頻
ExpiresByType image / gif“access plus 1 month”
ExpiresByType image / png“access plus 1 month”
ExpiresByType image / jpg“access plus 1 month”
ExpiresByType image / jpeg“access plus 1 month”
ExpiresByType視頻/ ogg“訪問(wèn)加1個(gè)月”
ExpiresByType音頻/ ogg“訪問(wèn)加1個(gè)月”
ExpiresByType視頻/ mp4“訪問(wèn)加1個(gè)月”
#CSS和JavaScript
ExpiresByType text / css“access plus 1 year”
ExpiresByType應(yīng)用程序/ javascript“訪問(wèn)加1年”
ExpiresByType text / javascript“access plus 1 year”
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
3.啟用Keep-Alive以減少HTTP請(qǐng)求
通過(guò)啟用Keep-Alive,您的Web服務(wù)器告訴Web瀏覽器不需要為它在站點(diǎn)上檢索的每個(gè)文件單獨(dú)請(qǐng)求。此外,請(qǐng)確保以減少HTTP請(qǐng)求的方式對(duì)其進(jìn)行編碼。
<ifModule mod_headers.c> Header Set Connection Keep-alive <ifModule>
4.防止垃圾郵件機(jī)器人抓取您的網(wǎng)站
有時(shí),您的網(wǎng)站的頁(yè)面加載速度可能會(huì)因主機(jī)方案中可用的帶寬而降低。有時(shí)垃圾郵件船會(huì)占用您的大部分帶寬,而您的網(wǎng)站也會(huì)變慢。
因此,防止垃圾郵件是有幫助的。
<ifModule mod_setenvif.c>
#將垃圾郵件發(fā)送者引薦為spambot
SetEnvifNoCase Referer spambot1.com spambot=yes
SetEnvifNoCase Referer spambot1.com spambot=yes
#添加任意數(shù)量的內(nèi)容
Order allow,deny
Allow from all
Deny from env=spambot
<ifModule>
因此,我們可以將.htaccess文件用于多種用途。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。