Copy string from a position

1

I have this:

private List<string> _listaCommiter()
        {
            string s = string.Empty;
            string _start = ConfigurationSettings.AppSettings["dir_inicio"];
            List<string> lista = new List<string>();
            List<string> tes = new List<string>();

            string path = ConfigurationSettings.AppSettings["Caminho_Commiter"];

            string[] arquivos = Directory.GetFiles(path, "*", SearchOption.AllDirectories);

            string texto = string.Empty;

            foreach (var item in arquivos)
            {
                s = Path.GetFileNameWithoutExtension(item);
                int _int = item.ToString().IndexOf(_start);
                texto = item.ToString().Substring(_int, item.Length);

                if (!item.Contains("TSNMVC"))
                    lista.Add(s);
            }

            return lista;
        }

At the moment I put the text variable inside the foreach, it gives me this error:

  

Message = Index and length must refer to a location within the   string. Parameter name: length

Below the entire error message

  

System.ArgumentOutOfRangeException was unhandled HResult = -2146233086   Message = Index and length should refer to a location within the   string. Parameter name: length Source = mscorlib
  ParamName = length StackTrace:          in System.String.Substring (Int32 startIndex, Int32 length)          in CreatingExtracting_ZIP.Form1._listCommiter () in c: \ Projects_Amil \ CreatingExtraining_ZIP \ CreatingExtraining_ZIP \ Form1.cs: line   211          in CreatingExtracting_ZIP.Form1.button1_Click (Object sender, EventArgs, and) in   c: \ Projects_Amil \ CreatingExtracting_ZIP \ CreatingExtraining_ZIP \ Form1.cs: line   240          in System.Windows.Forms.Control.OnClick (EventArgs and)          in System.Windows.Forms.Button.OnClick (EventArgs and)          in System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent)          in System.Windows.Forms.Control.WmMouseUp (Message & m, MouseButtons button, Int32 clicks)          in System.Windows.Forms.Control.WndProc (Message & m)          in System.Windows.Forms.ButtonBase.WndProc (Message & m)          in System.Windows.Forms.Button.WndProc (Message & m)          at System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m)          in System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m)          in System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)          at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG & msg)          at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (IntPtr   dwComponentID, Int32 reason, Int32 pvLoopData)          at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32   reason, ApplicationContext context)          at System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32   reason, ApplicationContext context)          in System.Windows.Forms.Application.Run (Form mainForm)          in CreatingExtracting_ZIP.Program.Main () in c: \ Projects_Amil \ CreatingExtraining_ZIP \ CreatingExtraining_ZIP \ Program.cs: line   19          in System.AppDomain._nExecuteAssembly (RuntimeAssembly assembly, String [] args)          in System.AppDomain.ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args)          in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()          in System.Threading.ThreadHelper.ThreadStart_Context (Object state)          in System.Threading.ExecutionContext.RunInternal (ExecutionContext   executionContext, ContextCallback callback, Object state, Boolean   preserveSyncCtx)          in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean   preserveSyncCtx)          in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state)          in System.Threading.ThreadHelper.ThreadStart () InnerException:

    
asked by anonymous 01.03.2016 / 00:12

1 answer

3

This error occurs because you are trying to retrieve a portion of the String that does not exist.

Consider the case

String item = "0123456789"; // o Length da sua String é 10;
int _int = 3; // inicio da Substring = 3

var texto = item.ToString().Substring(_int, item.Length);

When you do this you are removing the part of String starting at position 3 and finishing at the 10th position, ie the result of your String would be somewhere between the 3rd and 13th position of String , but its String only has 10 positions.

Soon you will have the message

  

Message = Index and length must refer to a location within the   string. Parameter name: length

using System;

 public class Sample
 {
    public static void Main () {
       Cordas myString = "abc";
       bool test1 = myString.Substring (2, 1) .Equals ( "C"); // Isso é verdade.
       Console.WriteLine (test1);
       bool test2 = String.IsNullOrEmpty (myString.Substring (3, 0)); // Isso é verdade.
       Console.WriteLine (test2);
       try {
          cadeia str3 = myString.Substring (3, 1); // Isto lança ArgumentOutOfRangeException.
          Console.WriteLine (str3);
       }
       catch (ArgumentOutOfRangeException e) {
          Console.WriteLine (e.Message);
       }         
    }
 }
 // O exemplo mostra o seguinte resultado:
 // Verdade
 // Verdade
 // Índice e comprimento devem se referir a um local dentro da cadeia.
 // Nome do parâmetro: comprimento

More Details Here .

    
01.03.2016 / 12:16