【问题标题】:Need Help the best overloaded method match for 'string.endswith(string)' has some invalid arguments需要帮助 'string.endswith(string)' 的最佳重载方法匹配有一些无效参数
【发布时间】:2015-12-11 07:15:54
【问题描述】:
dynamic counter = 1;
string FileNameWithoutExtestion = "";
FileNameWithoutExtestion = file.Split('.')[0];
string FileExtestion = file.Split('.')[1];
while (System.IO.File.Exists(Dir + file))
{
    if (true)
    {
        counter = counter + 1;
        if (FileNameWithoutExtestion.EndsWith('_'))
        {
            file = FileNameWithoutExtestion + counter.ToString() + "." + FileExtestion;
        }
        else
        {
            file = FileNameWithoutExtestion + "_" + counter.ToString() + "." + FileExtestion;
        }
    }
}

if (FileNameWithoutExtestion.EndsWith('_')) //这里发生错误

怎么了?

【问题讨论】:

  • 旁注:if (true) 完全无用,删除即可
  • 旁注:你可能想要var counter = 1;不是dynamic counter = 1;
  • @Fubo 是正确的,请参阅工作演示 Ideone。还有为什么 dynamic 为什么不 int ?在这里,您只是递增名为 counter 的变量。

标签: c# asp.net


【解决方案1】:

String.EndsWith() 仅具有以string 作为参数的重载,您插入char

替换

.EndsWith('_')

.EndsWith("_")

我会使用这些路径方法来解析文件名和扩展名

string FileNameWithoutExtestion = System.IO.Path.GetFileNameWithoutExtension(file);
string FileExtestion = System.IO.Path.GetExtension(file); //.jpg

因为FileNameWithoutExtestion = file.Split('.')[0]; 会在foo.bar.jpg 这样的文件名的情况下导致无效值

【讨论】:

  • 用.EndsWith("_")替换后出现同样的问题
  • 不可能 - 请交叉检查您的代码。可能你在另一个地方有这个电话
  • 工作.. 谢谢@fubo
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2014-04-10
  • 2015-10-14
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多