Console Application stops debugging when setting a value to a variable and replacing it

1

I have started building a simple console application and ran into some weird behavior. I have isolated the problem to the below code. For some reason when the the s.replace line is executed I immediately see this in the output: "'appname.vshost.exe' (Managed): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'" and then "The program '[17680] appname.vshost.exe: Managed' has exited with code -2147023895 (0x800703e9)."

Debugging is stopped. The value in the arg for path is the full UNC path where a file is located. I want to strip the path= and use the path value. Did I skip something somewhere and VS isnt giving me an exception? I've used VS 2008 and 2010 both with the same issue. Is this because the s is an arg?

            foreach (string s in args)
            {
                if ((s != "") && (s.ToString().ToLower().Contains("path=")))
                {
                    string a = @"\\computer\dir\";                            
                    a.Replace("path=", "");
                }
            }

This may be a visual studio issue because it appears to happen on any string I assign a value. I simply added the below removing the replace and got the same response:

string a = @"value";

Path example is \computer\directory1\directory2\

I've updated the code based on suggestions but the above still has the same problem. Fails on the replace line of code.

c#
asked on Stack Overflow Feb 3, 2016 by Mike • edited Feb 3, 2016 by Mike

1 Answer

2

I don't know if it's the source of your error, but string.Replace returns a new string - it does not modify the underlying string. Plus you are checking for a null value after you check if the string contains a particular substring. The proper loop if you want to update the strings in the collection would be:

for(int i=0; i < args.Length; args++)
{
    s = args[i];
    if (s != null && s.ToLower().Contains("path="))
         args[i] = s.Replace("path=", "");
    else
         throw new Exception("Missing file path in command line");
}
answered on Stack Overflow Feb 3, 2016 by D Stanley

User contributions licensed under CC BY-SA 3.0