InfinityGoesAround
InfinityGoesAround

Reputation: 1021

Start index cannot be larger than the length of the string

I'm trying to check if a foldername doesn't have a extension.
Because I want to change the name of the folder.
But you can also change the name of a file.

And I get this error:

"startIndex cannot be larger than length of string.\r\nParameter name: startIndex"}

By this line:

 string newFilenameExtension = Path.GetExtension(model.FileName.Trim()).Substring(1);

How to check if the foldername has no extension?

Upvotes: 0

Views: 13083

Answers (1)

ramiramilu
ramiramilu

Reputation: 17182

You should have code in following way -

string newFilenameExtension = Path.GetExtension("Sample".Trim());
string extn = string.Empty;

if (!String.IsNullOrWhiteSpace(newFilenameExtension))
{
      extn = newFilenameExtension.Substring(1);
}

if(!String.IsNullOrWhiteSpace(extn))
{
      // Use extn here
}

Upvotes: 1

Related Questions