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.

Mike
  • 419
  • 1
  • 6
  • 22
  • Could it be that you're missing an opening curly brace `{` after your `else` (before the `throw`)? – user2023861 Feb 03 '16 at 19:09
  • So far looks like duplicate of "C# String replace does not work". If you can provide [MCVE] that is different than `void Main(string[]args){}` - this could be reopened as different problem. – Alexei Levenkov Feb 03 '16 at 19:10
  • 1
    Couple of things that seem strange: You are calling `ToLower()` on `s` BEFORE you are checking if it is null. Also, you are not assigning a value to the return of your `Replace` call. While that doesn't cause an exception, you are essentially throwing away the result of the method call. None of these explain the problem you're having, which is why this is just a comment and not an answer. – Brian Ball Feb 03 '16 at 19:10
  • @BrianBall `args` (assuming arguments to `Main`) will not have null strings - so no need to check for null there. – Alexei Levenkov Feb 03 '16 at 19:11
  • No you can do a 1 line if else statement without a brace. I added a sample path. It is replacing the path= without an issue unlike the link provided above. The problem is the abnormal exiting of debugging. – Mike Feb 03 '16 at 19:12
  • Good catch on the tolower(). I've updated it to check for a null string first. I was simply trying to handle a null/empty command line. – Mike Feb 03 '16 at 19:19
  • Please show all relevant code, and how you debug this application. The exit code indicates a StackOverflowException. – CodeCaster Feb 03 '16 at 19:29
  • I changed the question so it's not a duplicate.. The problem appears to have nothing to do with replace(). I comment out replace and the same issue is present. The code is the issue the only thing not shown is that that it is in static void Main(string[] args) within a class and namespace. – Mike Feb 03 '16 at 19:32
  • A web search for the error number (800703e9) indicates that it's a [Fatal Execution Engine Error](https://social.msdn.microsoft.com/Forums/vstudio/en-US/9a864579-1449-47d7-8f4b-bbaa45d61be6/fatal-execution-engine-error-7a0f3cd5-800703e9?forum=clr). – Bob Jarvis - Слава Україні Feb 04 '16 at 01:01

1 Answers1

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");
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240