Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 追加文件名返回不正确的字符串_C# - Fatal编程技术网

C# 追加文件名返回不正确的字符串

C# 追加文件名返回不正确的字符串,c#,C#,我目前正在尝试向文件名添加日期时间戳记、前缀和唯一数字。我期望的输出是: \ParentDirectory\subdirectory\other subdirectory\Prefix-唯一编号-11 29 2016 2 07 30 PM.xlsx 上述前缀和唯一编号将被传递到函数中。我使用以下方法来实现这一点: public static string AppendDateTimeToFileName(this string fileName, string prefix, string un

我目前正在尝试向文件名添加
日期时间
戳记、前缀和唯一数字。我期望的输出是:

\ParentDirectory\subdirectory\other subdirectory\Prefix-唯一编号-11 29 2016 2 07 30 PM.xlsx

上述
前缀
唯一编号
将被传递到函数中。我使用以下方法来实现这一点:

public static string AppendDateTimeToFileName(this string fileName, string prefix, string uniqueNumber)
{
    return string.Concat(
        Path.GetFullPath(fileName),
        Path.Combine(prefix + " - " + uniqueNumber + " - "),
        Path.GetFileNameWithoutExtension(fileName),
        DateTime.Now.ToString()
        .Replace("/", " ")
        .Replace(":", " ")
        .Trim(),
        Path.GetExtension(fileName)
        );
}
我将上述方法称为:

string fileName = @"\\ParentDirectory\Sub Directory\Another Sub Directory\MyFile.xlsx";
string adjustedFileName = fileName.AppendDateTimeToFileName("Shipping Note", "0254900");
我收到的输出如下:

\ParentDirectory\subdirectory\other subdirectory\Shipping Note-\0254900-11 29 2016 2 08 10 PM


正如您在上面的输出中看到的,字符串是不正确的,首先我得到了一个额外的
-\
,文件扩展名也没有通过。有人能告诉我哪里出了问题吗。

发布的代码版本给出了以下输出:

\ParentDirectory\subdirectory\other subdirectory\MyFile.xlsx发货通知-0254900-MyFile29 11 2016 15 46 48.xlsx

而不是像你贴的那样:

\ParentDirectory\subdirectory\other subdirectory\Shipping Note-\0254900-11 29 2016 2 08 10 PM

如果您想要的输出是:

\ParentDirectory\subdirectory\other subdirectory\Prefix-唯一编号-11 29 2016 2 07 30 PM.xlsx

您需要将目录移动到
路径中。合并
并使用
GetDirectoryName
。另外,请删除该行:

Path.GetFileNameWithoutExtension(fileName)
因为在您想要的输出中,我看不到旧文件名
“MyFile”

此代码:

public static string AppendDateTimeToFileName(this string fileName, string prefix, string uniqueNumber)
{
    return string.Concat(               
        Path.Combine(Path.GetDirectoryName(fileName), prefix + " - " + uniqueNumber + " - "),
        DateTime.Now.ToString()
        .Replace(".", " ")
        .Replace(":", " ")
        .Trim(),
        Path.GetExtension(fileName)
        );
}
将产生以下输出:

\ParentDirectory\subdirectory\other subdirectory\Shipping Note-0254900-29 11 2016 15 39 37.xlsx

我会这样做的

public static string AppendDateTimeToFileName(this string fileName, string prefix, string uniqueNumber)
{
    return Path.Combine(
        Path.GetDirectoryName(fileName),
        string.Concat(
            prefix,
            " - ",
            uniqueNumber,
            " - ",
            Path.GetFileNameWithoutExtension(fileName),
            DateTime.Now.ToString("MM dd yyyy h mm ss tt"),
            Path.GetExtension(fileName)));
}

这正确地使用了
Path.Combine
来组合
Path.GetDirectoryName
中的目录,您就得到了新的连接文件名。注意,我还使用了日期格式字符串,而不是替换字符串。您可能需要考虑更改格式并在文件名和日期之间设置一个分隔符。< /P>路径。GetFullPath(文件名)返回完整路径而不是文件名。@安东尼兰伯特:好吧,但是我如何在返回值中得到一个额外的‘-`?此外,我还希望返回完整路径和附加文件,因为稍后我会使用新名称将文件保存到路径中,您不需要
path。合并
只是将前缀和数字连接在一起
Path.Combine
用于将路径的一部分与适当的路径分隔符组合。您的方法有一个名为
uniqueNumber
的参数,但您使用了一个名为
rmaNumber
的参数。当我修正我的结果是“\\ParentDirectory\subdirectory\other subdirectory\MyFile.xlsx Shipping Note-0254900-MyFile11 29 2016 9 38 04 AM.xlsx”@juharr很抱歉,这是我尝试另一个参数时的一个输入错误。我刚刚尝试了这个,效果非常好!非常感谢。只是一个简短的提示,我将这一行
.Replace(“.”,”)
更新为
.Replace(“/”,”)