本文整理汇总了C#中System.String.IsNullOrWhiteSpace方法的典型用法代码示例。如果您正苦于以下问题:C# String.IsNullOrWhiteSpace方法的具体用法?C# String.IsNullOrWhiteSpace怎么用?C# String.IsNullOrWhiteSpace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.IsNullOrWhiteSpace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReleaseFolder
/// <summary>释放文件夹</summary>
/// <param name="asm"></param>
/// <param name="prefix"></param>
/// <param name="dest"></param>
/// <param name="overWrite"></param>
/// <param name="filenameResolver"></param>
public static void ReleaseFolder(Assembly asm, String prefix, String dest, Boolean overWrite, Func<String, String> filenameResolver)
{
if (asm == null) asm = Assembly.GetCallingAssembly();
// 找到符合条件的资源
String[] names = asm.GetManifestResourceNames();
if (names == null || names.Length < 1) return;
IEnumerable<String> ns = null;
if (prefix.IsNullOrWhiteSpace())
ns = names.AsEnumerable();
else
ns = names.Where(e => e.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
if (String.IsNullOrEmpty(dest)) dest = AppDomain.CurrentDomain.BaseDirectory;
if (!Path.IsPathRooted(dest))
{
String str = Runtime.IsWeb ? HttpRuntime.BinDirectory : AppDomain.CurrentDomain.BaseDirectory;
dest = Path.Combine(str, dest);
}
// 开始处理
foreach (String item in ns)
{
Stream stream = asm.GetManifestResourceStream(item);
// 计算filename
String filename = null;
// 去掉前缀
if (filenameResolver != null) filename = filenameResolver(item);
if (String.IsNullOrEmpty(filename))
{
filename = item;
if (!String.IsNullOrEmpty(prefix)) filename = filename.Substring(prefix.Length);
if (filename[0] == '.') filename = filename.Substring(1);
String ext = Path.GetExtension(item);
filename = filename.Substring(0, filename.Length - ext.Length);
filename = filename.Replace(".", @"\") + ext;
filename = Path.Combine(dest, filename);
}
if (File.Exists(filename) && !overWrite) return;
String path = Path.GetDirectoryName(filename);
if (!path.IsNullOrWhiteSpace() && !Directory.Exists(path)) Directory.CreateDirectory(path);
try
{
if (File.Exists(filename)) File.Delete(filename);
using (FileStream fs = File.Create(filename))
{
IOHelper.CopyTo(stream, fs);
}
}
catch { }
finally { stream.Dispose(); }
}
}
示例2: DecodeFile
/// <summary>
/// Decrypt the encoded file and compare to original file. It returns false if the file is corrupted.
/// </summary>
/// <param name="key">The key used to encode the file</param>
/// <param name="sourceFile">The file to decrypt complete path</param>
/// <param name="destFile">Destination file complete path. If the file doesn't exist, it creates it</param>
/// <returns></returns>
public bool DecodeFile(string key, String sourceFile, String destFile)
{
if (sourceFile.IsNullOrWhiteSpace() || !File.Exists(sourceFile))
throw new FileNotFoundException("Cannot find the specified source file", sourceFile ?? "null");
if (destFile.IsNullOrWhiteSpace())
throw new ArgumentException("Please specify the path of the output path", nameof(destFile));
if (string.IsNullOrEmpty(key))
throw new ArgumentException("Please specify the key", nameof(key));
// Create a key using a random number generator. This would be the
// secret key shared by sender and receiver.
byte[] secretkey = key.ToByteArray();
// Initialize the keyed hash object.
HMACMD5 hmacMD5 = new HMACMD5(secretkey);
// Create an array to hold the keyed hash value read from the file.
byte[] storedHash = new byte[hmacMD5.HashSize / 8];
// Create a FileStream for the source file.
FileStream inStream = new FileStream(sourceFile, FileMode.Open);
// Read in the storedHash.
inStream.Read(storedHash, 0, storedHash.Length);
// Compute the hash of the remaining contents of the file.
// The stream is properly positioned at the beginning of the content,
// immediately after the stored hash value.
byte[] computedHash = hmacMD5.ComputeHash(inStream);
// compare the computed hash with the stored value
int i;
for (i = 0; i < storedHash.Length; i++)
{
if (computedHash[i] != storedHash[i])
{
inStream.Close();
return false;
}
}
FileStream outStream = new FileStream(destFile, FileMode.Create);
// Reset inStream to the beginning of the file.
inStream.Position = i;
// Copy the contents of the sourceFile to the destFile.
int bytesRead;
// read 1K at a time
byte[] buffer = new byte[1024];
do
{
// Read from the wrapping CryptoStream.
bytesRead = inStream.Read(buffer, 0, 1024);
outStream.Write(buffer, 0, bytesRead);
} while (bytesRead > 0);
// Close the streams
inStream.Close();
outStream.Close();
return true;
}
示例3: DecodeString
/// <summary>
/// Decode a string encoded in Base64 format
/// </summary>
/// <param name="sourceString">The encoded string to decode</param>
/// <returns>The decoded string</returns>
public string DecodeString(String sourceString)
{
if (!sourceString.IsNullOrWhiteSpace())
{
byte[] filebytes = Convert.FromBase64String(sourceString);
return Utils.ByteArrayToStr(filebytes);
}
else
return string.Empty;
}
示例4: EncodeString
/// <summary>
/// Encode a string using Base64 format
/// </summary>
/// <param name="sourceString">The source string to encode</param>
/// <returns>The encoded string</returns>
public string EncodeString(String sourceString)
{
if (!sourceString.IsNullOrWhiteSpace())
{
byte[] filebytes = sourceString.ToByteArray();
return Convert.ToBase64String(filebytes);
}
else
return string.Empty;
}
示例5: AddTorrentFromData
public void AddTorrentFromData(Byte[] torrentData, String downloadDirectory, TransmissionSettings settings)
{
var arguments = new Dictionary<String, Object>();
arguments.Add("metainfo", Convert.ToBase64String(torrentData));
if (!downloadDirectory.IsNullOrWhiteSpace())
{
arguments.Add("download-dir", downloadDirectory);
}
ProcessRequest("torrent-add", arguments, settings);
}
示例6: AddTorrentFromUrl
public void AddTorrentFromUrl(String torrentUrl, String downloadDirectory, TransmissionSettings settings)
{
var arguments = new Dictionary<String, Object>();
arguments.Add("filename", torrentUrl);
if (!downloadDirectory.IsNullOrWhiteSpace())
{
arguments.Add("download-dir", downloadDirectory);
}
ProcessRequest("torrent-add", arguments, settings);
}
示例7: DecodeFile
/// <summary>
/// Decode a File encoded in Base64 format
/// </summary>
/// <param name="sourceFile">The file to decrypt complete path</param>
/// <param name="destFile">Destination file complete path. If the file doesn't exist, it creates it</param>
public void DecodeFile(String sourceFile, String destFile)
{
if (sourceFile.IsNullOrWhiteSpace() || !File.Exists(sourceFile))
throw new FileNotFoundException("Cannot find the specified source file", sourceFile ?? "null");
if (destFile.IsNullOrWhiteSpace())
throw new ArgumentException("Please specify the path of the output path", nameof(destFile));
string input = File.ReadAllText(sourceFile);
byte[] filebytes = Convert.FromBase64String(input);
FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None);
fs.Write(filebytes, 0, filebytes.Length);
fs.Close();
}
示例8: EncodeFile
/// <summary>
/// Encode a File using Base64 format
/// </summary>
/// <param name="sourceFile">The file to encrypt complete path</param>
/// <param name="destFile">Destination file complete path. If the file doesn't exist, it creates it</param>
public void EncodeFile(String sourceFile, String destFile)
{
if (sourceFile.IsNullOrWhiteSpace() || !File.Exists(sourceFile))
throw new FileNotFoundException("Cannot find the specified source file", sourceFile ?? "null");
if (destFile.IsNullOrWhiteSpace())
throw new ArgumentException("Please specify the path of the output path", nameof(destFile));
using (var fs = new BufferedStream(File.OpenRead(sourceFile), 1200000))
{
byte[] filebytes = new byte[fs.Length];
fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
string encodedData = Convert.ToBase64String(filebytes, Base64FormattingOptions.InsertLineBreaks);
File.WriteAllText(destFile, encodedData);
}
}
示例9: TryGetMessageBusType
/// <summary>
/// Gets a message bus type based on the specified string <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to convert to an message bus type (i.e., name, value or unique description).</param>
/// <param name="result">The message bus type.</param>
public static Boolean TryGetMessageBusType(String value, out MessageBusType result)
{
Int32 parsedValue;
Object boxedValue = Int32.TryParse(value, out parsedValue) ? parsedValue : (Object)value;
if (value.IsNullOrWhiteSpace())
{
result = MessageBusType.MicrosoftMessageQueuing;
return true;
}
if (Enum.IsDefined(typeof(MessageBusType), boxedValue))
{
result = (MessageBusType)Enum.ToObject(typeof(MessageBusType), boxedValue);
return true;
}
return KnownMessageBusTypes.TryGetValue(value, out result);
}
示例10: FileNameStr
/// <summary>工具方法:生成年月日时分秒字符串</summary>
/// <param name="link">年月日时分秒之间的连接字符</param>
/// <param name="RanLength">最后生成随机数的位数</param>
/// <returns>返回年月日时分秒毫秒四位随机数字的字符串</returns>
public static String FileNameStr(String link, Int32 RanLength)
{
if (link.IsNullOrWhiteSpace())
{
link = "";
}
Int32 Year = DateTime.Now.Year; //年
Int32 Month = DateTime.Now.Month; //月份
Int32 Day = DateTime.Now.Day; //日期
Int32 Hour = DateTime.Now.Hour; //小时
Int32 Minute = DateTime.Now.Minute; //分钟
Int32 Second = DateTime.Now.Second; //秒
Int32 Milli = DateTime.Now.Millisecond; //毫秒
//生成随机数字
String DataString = "0123456789";
Random rnd = new Random();
String rndstring = "";
Int32 i = 1;
while (i <= RanLength)
{
rndstring += DataString[rnd.Next(DataString.Length)];
i++;
}
String FileNameStr = (Year + link + Month + link + Day + link + Hour + link + Minute + link + Second + link + Milli + link + rndstring).ToString();
return FileNameStr;
}
示例11: UpdateLibrary
public String UpdateLibrary(XbmcSettings settings, String path)
{
var request = new RestRequest();
var parameters = new Dictionary<String, Object>();
parameters.Add("directory", path);
if (path.IsNullOrWhiteSpace())
{
parameters = null;
}
var response = ProcessRequest(request, settings, "VideoLibrary.Scan", parameters);
return Json.Deserialize<XbmcJsonResult<String>>(response).Result;
}
示例12: ArrangeAddressSet
private void ArrangeAddressSet(AddressSet addressSet, String street, String postCode, String houseNumber, String apartmentNumber, String city)
{
GetWordsFromField(addressSet.wordComponents, city.PrepareString(WORDSTOREMOVE));
GetWordsFromField(addressSet.wordComponents, street.PrepareString(WORDSTOREMOVE));
if (!postCode.IsNullOrWhiteSpace())
{
String tempCode = postCode.GetOnlyDigits();
if (tempCode.Length == 5)
{
addressSet.postalCodeComponent = tempCode.Substring(0, 2) + " " + tempCode.Substring(2, 3);
}
}
if (!String.IsNullOrEmpty(houseNumber))
{
addressSet.houseNumberComponent = houseNumber.PrepareString();
}
if (!String.IsNullOrEmpty(apartmentNumber))
{
addressSet.apartmentNumberComponent = apartmentNumber.PrepareString();
}
}
示例13: GetWordsFromField
private void GetWordsFromField(List<String> set, String input)
{
if (!input.IsNullOrWhiteSpace())
{
if (input.Trim().Contains(" ") || input.Trim().Contains("-"))
{
String[] split = input.Split(new char[] {' ', '-'} ,StringSplitOptions.RemoveEmptyEntries);
foreach (String piece in split)
{
if (!piece.IsNullOrWhiteSpace())
{
set.Add(piece);
}
}
}
else
{
set.Add(input);
}
}
}
示例14: Parse
/// <summary>分析</summary>
/// <param name="uri"></param>
public NetUri Parse(String uri)
{
if (uri.IsNullOrWhiteSpace()) return this;
// 分析协议
var p = uri.IndexOf(Sep);
if (p > 0)
{
Protocol = uri.Substring(0, p);
uri = uri.Substring(p + Sep.Length);
}
// 分析端口
p = uri.LastIndexOf(":");
if (p >= 0)
{
var pt = uri.Substring(p + 1);
Int32 port = 0;
if (Int32.TryParse(pt, out port))
{
Port = port;
uri = uri.Substring(0, p);
}
}
Host = uri;
return this;
}
示例15: Split
/// <summary>拆分排序字句</summary>
/// <param name="orderby"></param>
/// <param name="isdescs"></param>
/// <returns></returns>
public static String[] Split(String orderby, out Boolean[] isdescs)
{
isdescs = null;
if (orderby.IsNullOrWhiteSpace()) return null;
//2014-01-04 Modify by Apex
//处理order by带有函数的情况,避免分隔时将函数拆分导致错误
foreach (Match match in Regex.Matches(orderby, @"\([^\)]*\)", RegexOptions.Singleline))
{
orderby = orderby.Replace(match.Value, match.Value.Replace(",", "★"));
}
String[] ss = orderby.Trim().Split(",");
if (ss == null || ss.Length < 1) return null;
String[] keys = new String[ss.Length];
isdescs = new Boolean[ss.Length];
for (int i = 0; i < ss.Length; i++)
{
String[] ss2 = ss[i].Trim().Split(' ');
// 拆分名称和排序,不知道是否存在多余一个空格的情况
if (ss2 != null && ss2.Length > 0)
{
keys[i] = ss2[0].Replace("★", ",");
if (ss2.Length > 1 && ss2[1].EqualIgnoreCase("desc")) isdescs[i] = true;
}
}
return keys;
}