[英]Eliminate file extension with GetFileNameWithoutExtension
I used this method Path.GetFileNameWithoutExtension
to eleminate file extension from string like from "abc:file.jpg" to "abc:file" but when i do this 我使用这个方法
Path.GetFileNameWithoutExtension
从字符串中Path.GetFileNameWithoutExtension
文件扩展名,例如从“abc:file.jpg”到“abc:file”但是当我这样做时
Path.GetFileNameWithoutExtension("abc:file.jpg")
it shows the result as "file". 它将结果显示为“文件”。 It also removed "abc:".
它还删除了“abc:”。 Why has it happened ?
为什么会这样? Is there any better way to get this problem solved?
有没有更好的方法来解决这个问题?
The char ":" (colon) is not legal as part of a filename. char“:”(冒号)作为文件名的一部分是不合法的。 It interpret it as a path divider so it will remove it and anything before it when you request only the filename.
它将其解释为路径分隔符,因此当您仅请求文件名时,它将删除它和之前的任何内容。
Here is one way you can check if the filename is valid: 以下是检查文件名是否有效的一种方法:
This function will return true for valid filenames, and false for invalid ones: 对于有效的文件名,此函数将返回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;
}
If the above function returns false you can use the next function to format the filename and remove the illegal chars (there exists os-functions to do this IIRC, but it's a simple exercise to do manually): 如果上面的函数返回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;
}
Example of usage: 用法示例:
private void Button1_Click(System.Object sender, System.EventArgs e)
{
string filename = "abc:file.jpg";
if (!IsValidFilename(filename)) {
filename = MakeFilenameValid(filename, "_");
}
MessageBox.Show(filename);
}
In VB: 在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
It's better to name your files properly like abcfile.jpg or abc_file.jpg in the first place. 最好将文件命名为abcfile.jpg或abc_file.jpg。 then you'll be able to get complete file name.
那么你将能够获得完整的文件名。
试试这种方式..
Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.