在日常的工作中,有时候需要将部分信息保存到本地,也就是写入到INI文件中,便于下次使用;在Qt中可以使用QSetting来进行INI文件的读写。
(1).void QSettings::beginGroup(const QString & prefix)
相当于在INI文件中添加了一个索引,根据索引的不同,可以读取相对应的值
(2).void QSettings::endGroup()
将组重置为相应的eginGroup()调用之前的状态。
(3).void QSettings::setValue(const QString & key, const QVariant & value)
向INI文件中写入信息,信息以键值对的方式写入到INI文件中,如果key值已经存在了,那么key对应的value值便会被新值覆盖掉。
(4).QVariant QSettings::value(const QString & key, const QVariant & defaultValue=QVariant()) const
从INI文件中读取信息,返回设置键的值,如果设置的键不存在,则返回defaultValue,如果未指定默认的值,则返回默认QVariant()。
(5).QString QSettings::group() const
返回当前的组名
Qt开发必备技术栈学习路线和资料
.NET 中,有多种配置文件格式和读取机制可用于管理应用程序的设置和配置信息。这包括JSON、XML、INI格式的配置文件,以及环境变量。下面我们将分别探讨如何轻松读取这些配置文件和环境变量。
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。在.NET 中,可以使用 System.Text.Json 命名空间下的类来读取 JSON 配置文件。
例如,假设你有一个名为 appsettings.json 的配置文件,内容如下:
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)是一种标记语言,可以用来创建自定义的标记来描述存储的数据。在.NET 中,可以使用 System.Xml 命名空间下的类来读取 XML 配置文件。
例如,假设你有一个名为 app.config 的 XML 配置文件,内容如下:
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);
}
}
请注意,XML 配置文件通常需要添加在项目中的 App.config 或 Web.config 文件中,并且需要引用 System.Configuration 程序集。
htaccess文件是运行Apache Web Server的Web服务器的配置文件,对配置和重定向Apache Web Server文件系统很有用。
请记住.htaccess文件将采用隐藏格式。没有人可以通过URL直接看到它。
.htaccess文件有很多用途。在这里,我将讨论创建它们及其用途。
打开任何文本编辑器并使用名称保存文件.htaccess。mod_rewrite在Apache Web服务器配置中的php.ini文件中启用扩展。
1.禁用目录列表
如果要禁用文件夹文件列表,请包括以下代码。
#禁用目录列表
Options All –Indexes
2.错误页面
您可以为特定错误设置错误页面。
示例:
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服务器中启用“重写规则”,则必须编写:RewriteEngine on
如果要关闭此规则,请将值更改为关闭。
RewriteEngine on
3.域重定向
要将yourdomain.com永久重定向到www.yourdomain.com,请添加以下代码:
RewriteCond%{HTTP_HOST} ^ yourdomain.com
RewriteRule(。*)http://www.yourdomain.com / $ 1 [R=301,L]
4.子域重定向
您还可以执行子文件重定向映射到该文件夹。这里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是搜索引擎优化过程中非常重要的一部分。
配置文件参数[ a-zA-Z0-9_- ]会打开此输入(有关更多帮助,请阅读正则表达式):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
这里第一个参数允许[a-zA-Z0-9_-],第二个参数只允许数字[ 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.隐藏文件扩展名
假设您不想显示网页扩展名。
示例:
http://www.yourdomain.com/index.html至 http://www.yourdomain.com/index只需添加以下代码:
RewriteRule ^([^ /。] +)/?$ $ 1.html
1.启用压缩:
压缩对于减小网页大小非常有用。
有两种压缩选项:
要启用Deflate模式,请在.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.添加过期标题:启用浏览器缓存
Expire标头用于缓存来自浏览器的数据。加速网页是有帮助的,因为网页可以从浏览器中检索数据,因此无需从减少HTTP请求的服务器获取数据。
<IfModule mod_expires.c>
ExpiresActive on
#如果要设置默认过期日期
ExpiresDefault“访问加1个月”
#对于html文档
ExpiresByType text / html“access plus 0 seconds”
#数据
ExpiresByType text / xml“access plus 0 seconds”
ExpiresByType application / xml“access plus 0 seconds”
ExpiresByType应用程序/ json“访问加0秒”
# RSS订阅
ExpiresByType应用程序/ rss + xml“访问加1小时”
#Favicon(无法重命名)
ExpiresByType image / x-icon“访问加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“访问加1个月”
ExpiresByType音频/ ogg“访问加1个月”
ExpiresByType视频/ mp4“访问加1个月”
#CSS和JavaScript
ExpiresByType text / css“access plus 1 year”
ExpiresByType应用程序/ javascript“访问加1年”
ExpiresByType text / javascript“access plus 1 year”
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>
3.启用Keep-Alive以减少HTTP请求
通过启用Keep-Alive,您的Web服务器告诉Web浏览器不需要为它在站点上检索的每个文件单独请求。此外,请确保以减少HTTP请求的方式对其进行编码。
<ifModule mod_headers.c> Header Set Connection Keep-alive <ifModule>
4.防止垃圾邮件机器人抓取您的网站
有时,您的网站的页面加载速度可能会因主机方案中可用的带宽而降低。有时垃圾邮件船会占用您的大部分带宽,而您的网站也会变慢。
因此,防止垃圾邮件是有帮助的。
<ifModule mod_setenvif.c>
#将垃圾邮件发送者引荐为spambot
SetEnvifNoCase Referer spambot1.com spambot=yes
SetEnvifNoCase Referer spambot1.com spambot=yes
#添加任意数量的内容
Order allow,deny
Allow from all
Deny from env=spambot
<ifModule>
因此,我们可以将.htaccess文件用于多种用途。
*请认真填写需求信息,我们会在24小时内与您取得联系。