3

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

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

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

4

3 回答 3

4

字符“:”(冒号)作为文件名的一部分是不合法的。它将其解释为路径分隔符,因此当您仅请求文件名时,它将删除它以及它之前的任何内容。

这是检查文件名是否有效的一种方法:

此函数将对有效文件名返回 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-functions 来执行此 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
于 2012-12-19T06:16:55.743 回答
0

试试这个方法。。

Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path));
于 2012-12-19T06:26:38.467 回答
0

最好首先正确命名您的文件,如 abcfile.jpg 或 abc_file.jpg。然后您将能够获得完整的文件名。

于 2012-12-19T06:18:52.460 回答