CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2012
    Posts
    1

    c# "\" Unrecognized escape sequence *URGENT*

    Hello
    I have problem in Visual Studio 2005 C# that I cannot resolve.
    I have a path to my folder _putanja = @"images"; and I want
    to save one file in my case image file let say image_name.jpg in this folder.
    Now in new string I try to concatenate path with file (file will be in this folder) but when I try to concatenate like:

    _fotka = textBox8.Text.Trim(); //this is where I enter name of picture
    _slika = _putanja + @"\" + _fotka + ".jpg";

    when I debug I get \\ instead of \ backslash
    result is “slike\\image_name.jpg”, bit I want “slike\image_name.jpg” with just one backslash, because I will write this in DB and when I will read this string with 2 \\ I will get error!
    Any help, what is wrong with my code.
    Thanks

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: c# "\" Unrecognized escape sequence *URGENT*

    The simple explanation is the user has included a backslash in the textbox (e.g. "slike\").

    To fix this, you could prevent the user from entering a path with a backslash, but you probably don't want to do this because then the user couldn't enter something like "slike\temp" as a path.

    The proper way to do this is to check each component of the path prior to forming the whole path and strip off any unnecessary back slashes.

    Fortunately, Path.Combine takes the drudgery out of doing this manually. There are several method overloads that allow you to combine two or more path components.

    Code:
    _slika = Path.Combine( _putanja, String.Format( "{0}.jpg", _fotka ) )";
    In the above code, if _putanja contains a proper path, then <picture_name>.jpg will be appended to it.

    On caveat to the above code is that it doesn't detect if a user enters the .jpg extension into the textbox. The code as written will happily append a second ".jpg" to the picture name even if one already exists. If this is a concern to you, then modify the code to ensure only one .jpg extension ever gets added.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured