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.

9
  • Could it be that you're missing an opening curly brace { after your else (before the throw)? Commented Feb 3, 2016 at 19:09
  • So far looks like duplicate of "C# String replace does not work". If you can provide minimal reproducible example that is different than void Main(string[]args){} - this could be reopened as different problem. Commented Feb 3, 2016 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. Commented Feb 3, 2016 at 19:10
  • @BrianBall args (assuming arguments to Main) will not have null strings - so no need to check for null there. Commented Feb 3, 2016 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. Commented Feb 3, 2016 at 19:12

1 Answer 1

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");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why would this not work then? string a = @"\\computer\ftp\ftpinbound\client\BC\";
Not assigning a method return value will not crash an application as shown by OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.