本文整理汇总了C#中System.Text.Encoding.IfNull方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.IfNull方法的具体用法?C# Encoding.IfNull怎么用?C# Encoding.IfNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.Encoding
的用法示例。
在下文中一共展示了Encoding.IfNull方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FileReadAll
/// <summary>
/// 读取文件中的内容 线程安全
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="encoding">编码</param>
/// <returns>读取文件中的内容</returns>
public static string FileReadAll(string fileName, Encoding encoding = null) {
string text = string.Empty;
if (!FileDirectory.FileExists(fileName)) { return text; }
using (StreamReader reader = new StreamReader(fileName, encoding.IfNull(Encoding.UTF8)))
return reader.ReadToEnd();
}
示例2: FileRead
/// <summary>
/// 读取文件中的内容
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="encoding">编码</param>
/// <returns>读取文件中的内容</returns>
public static IList<string> FileRead(string fileName, Encoding encoding = null) {
string lineText = null; IList<string> txtTextArr = new List<string>();
if (!FileDirectory.FileExists(fileName)) { return txtTextArr; }
using (StreamReader reader = new StreamReader(fileName, encoding.IfNull(Encoding.UTF8))) {
while ((lineText = reader.ReadLine()).IsNotNull()) txtTextArr.Add(lineText);
}
return txtTextArr;
}
示例3: WriteForFileStream
/// <summary>
/// 写日志 线程安全
/// </summary>
/// <param name="fileName">文件</param>
/// <param name="str">消息</param>
/// <param name="encoding">编码</param>
private static bool WriteForFileStream(string fileName, string str, Encoding encoding = null) {
bool _isTrue = false;
if (encoding.IsNull()) encoding = Encoding.Default;
lock (lockHelper) {
using (FileStream fs = FileDirectory.FileExists(fileName) ? File.OpenWrite(fileName) : File.Create(fileName)) {
byte[] bt = encoding.IfNull(Encoding.UTF8).GetBytes(str + Environment.NewLine);
fs.Seek(0, SeekOrigin.End);
fs.Write(bt, 0, bt.Length);
_isTrue = true;
}
}
return _isTrue;
}
示例4: WriteForStreamWriter
/// <summary>
/// 写日志 线程安全
/// </summary>
/// <param name="fileName">文件</param>
/// <param name="str">消息</param>
/// <param name="encoding">编码</param>
private static bool WriteForStreamWriter(string fileName, string str, Encoding encoding = null) {
bool _isTrue = false;
if (encoding.IsNull()) encoding = Encoding.Default;
lock (lockHelper) {
using (System.IO.StreamWriter f2 = new System.IO.StreamWriter(fileName, true, encoding.IfNull(Encoding.UTF8))) { f2.WriteLine(str); _isTrue = true; }
}
return _isTrue;
}
示例5: SendGet
/// <summary>
/// 到指定的URL发送一个GET请求
/// </summary>
/// <param name="url">请求的URL</param>
/// <param name="contentType">请求的内容类型。默认值是'application/x-www-form-urlencoded'.</param>
/// <param name="credentials">使用的凭据</param>
/// <returns>返回请求的数据</returns>
public static string SendGet(string url, string contentType, ICredentials credentials, Encoding encoding = null) {
return SendRequest(url, null, "GET", contentType, null, null, credentials, null, null, encoding.IfNull(Encoding.UTF8));
}
示例6: SendPost
/// <summary>
/// POST数据
/// </summary>
/// <param name="url">URL</param>
/// <param name="postData">数据</param>
/// <param name="contentType">请求的内容类型。默认值是'application/x-www-form-urlencoded'</param>
/// <param name="credentials">使用的凭据</param>
/// <param name="timeout">超时</param>
/// <returns>返回请求的数据</returns>
public static string SendPost(string url, string postData, string contentType, ICredentials credentials, int? timeout, Encoding encoding = null) {
return SendRequest(url, postData, "POST", contentType, null, null, credentials, null, timeout, encoding.IfNull(Encoding.UTF8));
}