-2

I want to remove certain text from the Args, if the args contain that text.

args[1] = "C:\Users\Public\Roaming\Intel\Wireless\Settings\guids.xml"

I want to remove "\guids.xml" if the args[1] contains it. I tried something like below but of no luck.

if (args[1].ToUpper().Contains("GUIDS.XML"))
  {
    args[1] = args[1].Replace("GUIDS.XML","");
  }

Trying for an output like args[1] = "C:\Users\Public\Roaming\Intel\Wireless\Settings"

Note: I converted the input args to uppercase because I don't know how it was on the users machine.

I really appreciate any kind of suggestions.

  • 1
    `args[1]` isn't necessarily uppercase when you try to replace the uppercase substring... – sticky bit Feb 25 '20 at 20:36
  • 1
    if your first argument is file path and you wanted to retrieve directory from it, then you could use Path.GetDirectoryName(args[1]) which will give you the output you want – sam Feb 25 '20 at 20:38
  • 3
    `args[1] = Path.GetDirectoryName(args[1]);` to check `if (Path.GetFileName(args[1]).Equals("GUIDS.XML", StringComparison.OrdinalIgnoreCase)) {...}` – Dmitry Bychenko Feb 25 '20 at 20:39
  • Does this answer your question? [How do I get the directory from a file's full path?](https://stackoverflow.com/questions/674479/how-do-i-get-the-directory-from-a-files-full-path) – Md Hasan Ibrahim Feb 25 '20 at 20:41
  • @MdHasanIbrahim that indeed what OP likely *wants* to do. Please avoid suggesting duplicates that are absolutely not related to the question *as asked* as future visitors will not be able to use such duplicate. If you feel that question should be very different you can *edit* the post first and later add suggested duplicate so duplicate actually answers the question. – Alexei Levenkov Feb 25 '20 at 20:45

1 Answers1

-1

In my experience args are always in lower case when you get them, it dont conserve the user input case.

On your exemple you try to replace an uppercase string, so it is never replace.