![](/img/trans.png)
[英]System.IO.Path.GetFileNameWithoutExtension does not trim a file path
[英]Eliminate file extension with GetFileNameWithoutExtension
我使用這個方法Path.GetFileNameWithoutExtension
從字符串中Path.GetFileNameWithoutExtension
文件擴展名,例如從“abc:file.jpg”到“abc:file”但是當我這樣做時
Path.GetFileNameWithoutExtension("abc:file.jpg")
它將結果顯示為“文件”。 它還刪除了“abc:”。 為什么會這樣? 有沒有更好的方法來解決這個問題?
char“:”(冒號)作為文件名的一部分是不合法的。 它將其解釋為路徑分隔符,因此當您僅請求文件名時,它將刪除它和之前的任何內容。
以下是檢查文件名是否有效的一種方法:
對於有效的文件名,此函數將返回true,對於無效的文件名,此函數將返回false:
private bool IsValidFilename(string filename)
{
//
//-- Get array with invalid chars for filenames
//
char[] illegalChars = Path.GetInvalidFileNameChars;
//
//-- Go through each char in filename and check if the char is
// in our array of invalid chars
//
foreach (char c in filename) {
if (illegalChars.Contains(c))
return false;
}
//
//-- All are valid, return true
//
return true;
}
如果上面的函數返回false,你可以使用下一個函數格式化文件名並刪除非法字符(存在os函數來執行此IIRC,但這是一個簡單的練習,可以手動完成):
private string MakeFilenameValid(string filename, char replacment)
{
//
//-- Get array with invalid chars for filenames
//
char[] illegalChars = Path.GetInvalidFileNameChars;
StringBuilder validFilename = new StringBuilder();
//
//-- Go through each char in filename and check if the char is
// in our array of invalid chars. If it is, replace it
//
foreach (char c in filename) {
if (illegalChars.Contains(c)) {
validFilename.Append(replacment);
} else {
validFilename.Append(c);
}
}
//
//-- Return filename
//
return validFilename.ToString;
}
用法示例:
private void Button1_Click(System.Object sender, System.EventArgs e)
{
string filename = "abc:file.jpg";
if (!IsValidFilename(filename)) {
filename = MakeFilenameValid(filename, "_");
}
MessageBox.Show(filename);
}
在VB中:
Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
Dim filename As String = "abc:file.jpg"
If Not IsValidFilename(filename) Then
filename = MakeFilenameValid(filename, "_")
End If
MessageBox.Show(filename)
End Sub
Private Function IsValidFilename(filename As String) As Boolean
'
'-- Get array with invalid chars for filenames
'
Dim illegalChars() As Char = Path.GetInvalidFileNameChars
'
'-- Go through each char in filename and check if the char is
' in our array of invalid chars
'
For Each c As Char In filename
If illegalChars.Contains(c) Then Return False
Next
'
'-- All are valid, return true
'
Return True
End Function
Private Function MakeFilenameValid(filename As String, replacment As Char) As String
'
'-- Get array with invalid chars for filenames
'
Dim illegalChars() As Char = Path.GetInvalidFileNameChars
Dim validFilename As New StringBuilder
'
'-- Go through each char in filename and check if the char is
' in our array of invalid chars. If it is, replace it
'
For Each c As Char In filename
If illegalChars.Contains(c) Then
validFilename.Append(replacment)
Else
validFilename.Append(c)
End If
Next
'
'-- Return filename
'
Return validFilename.ToString
End Function
最好將文件命名為abcfile.jpg或abc_file.jpg。 那么你將能夠獲得完整的文件名。
試試這種方式..
Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.