You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Myshipping.Core
|
|
{
|
|
public static class StringExtentions
|
|
{
|
|
/// <summary>
|
|
/// 处理文件名称
|
|
/// </summary>
|
|
/// <param name="fn">文件名</param>
|
|
/// <returns>返回合法的文件名</returns>
|
|
public static string ValidFileName(this string fn)
|
|
{
|
|
char[] strs = "+#?*\"<>/;,-:%~".ToCharArray();
|
|
foreach (char c in strs)
|
|
fn = fn.Replace(c.ToString(), "_");
|
|
|
|
strs = ":,。;?".ToCharArray();
|
|
foreach (char c in strs)
|
|
fn = fn.Replace(c.ToString(), "_");
|
|
|
|
//去掉空格.
|
|
while (fn.Contains(" ") == true)
|
|
fn = fn.Replace(" ", "");
|
|
|
|
//替换特殊字符.
|
|
fn = fn.Replace("\t\n", "");
|
|
|
|
//处理合法的文件名.
|
|
StringBuilder rBuilder = new StringBuilder(fn);
|
|
foreach (char rInvalidChar in Path.GetInvalidFileNameChars())
|
|
rBuilder.Replace(rInvalidChar.ToString(), string.Empty);
|
|
|
|
fn = rBuilder.ToString();
|
|
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace("__", "_");
|
|
fn = fn.Replace(" ", "");
|
|
fn = fn.Replace(" ", "");
|
|
fn = fn.Replace(" ", "");
|
|
fn = fn.Replace(" ", "");
|
|
fn = fn.Replace(" ", "");
|
|
fn = fn.Replace(" ", "");
|
|
fn = fn.Replace(" ", "");
|
|
fn = fn.Replace(" ", "");
|
|
|
|
if (fn.Length > 240)
|
|
fn = fn.Substring(0, 240);
|
|
|
|
return fn;
|
|
}
|
|
}
|
|
}
|