使用GetFileNameWithoutExtension消除文件扩展名

时间:2012-12-19 06:13:29

标签: c# .net silverlight path file-extension

我使用此方法Path.GetFileNameWithoutExtension从字符串中删除文件扩展名,例如从“abc:file.jpg”到“abc:file”但是当我这样做时

Path.GetFileNameWithoutExtension("abc:file.jpg") 

它将结果显示为“文件”。它还删除了“abc:”。为什么会这样?有没有更好的方法来解决这个问题?

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

最好将文件命名为abcfile.jpg或abc_file.jpg。然后你就可以得到完整的文件名。

答案 2 :(得分:0)

试试这种方式..

Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path));