Results 1 to 7 of 7

Thread: [RESOLVED] Issues trying to use String.Replace?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Issues trying to use String.Replace?

    I'm trying to rename files in a directory. I'm using the @ sign to allow for the proper filepath to be loaded into a variable. Below is my code:

    Code:
            private void btnRenameImages_Click(object sender, EventArgs e)
            {
                foreach (ListViewItem item in lv1.Items)
                {
                    try
                    {
                        var newPath = txtNewImagePath.Text;
                        var filePath = item.ToString();
                        var filename = Path.GetFileNameWithoutExtension(item.ToString());
                        var newfilename = filename.Substring(0, (filename.Length - 3));
    
                        newPath = newPath + @"\" + newfilename + ".jpg";    //statement doesn't replace the "\\"
    
                        var a = filePath.Replace(@"\\", @"\");              //statement doesn't replace the "\\"
                        var b = newPath.Replace(@"\\", @"\");               //statement doesn't replace the "\\"
    
                        File.Move(@a, @b);             //Exception thrown on this line - "The given path's format is not supported"
    
                        lv2.Items.Add(b);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
    
            }
    The indicated lines are not working correctly.

    Thanks,
    Blake

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Issues trying to use String.Replace?

    I'm not sure what you're trying to accomplish, but since the "\" is an escape character and expects a second character to follow it to make one character, i.e. "\t" is tab, "\n" is newline, to actually reference a single "\" in a string you have to use two in a row "\\ " , so if you want to replace two "\\" with "\" (I haven't tested it but I would expect it would be Replace("\\\\", "\\") (not considering the @ if necessary).

    Likewise, if you want to append a "\" to the string it would be "\\".

    Of course, I don't program C#, so it is possible I'm not correct, but I'm kind of assuming it is similar to C and C++ in this regard, because you are using string literals here.
    Last edited by passel; May 10th, 2018 at 03:49 PM.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Issues trying to use String.Replace?

    Unfortunately your suggestion did not work. Still getting the same error.
    Blake

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Issues trying to use String.Replace?

    Your code doesn't really make sense. I'm not sure you understand exactly what that @ prefix does. It denotes a verbatim string literal. As such, it only makes sense on a string literal, so adding it to variables makes no sense. The point of the @ is that the '\' character in the literal is interpreted as a literal character and not an escape character. The point is that you then don't have to use double slashes in your literal. If you don't use the @ prefix then you do have to use double slashes in your literal, but that doesn't mean that there are double slashes in the actual string, so using String.Replace makes no sense. The double slashes is simply a representation.

    What is the actual aim here? It seems to be that you want to move every file listed to the current working directory, remove the last three characters from the file name and set the extension to ".jpg". If so, you never need to use a string literal so verbatim string literals are completely irrelevant. That said, I think you have a different issue. Have you checked what this actually returns?
    csharp Code:
    1. var filePath = item.ToString();
    I'm not sure what calling ToString on a ListViewItem returns but it may well not be what you want. That code should probably be:
    csharp Code:
    1. var filePath = item.Text;
    To then do as appears to be your plan with the file:
    csharp Code:
    1. string oldFilePath; // Old file path here.
    2. var oldFileName = Path.GetFileNameWithoutExtension(oldFilePath);
    3. var newFileName = oldFileName.Substring(0, oldFileName.Length - 3);
    4. var newFilePath = Path.Combine(Environment.CurrentDirectory, newFileName + ".jpg");
    5.  
    6. File.Move(oldFilePath, newFilePath);

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Issues trying to use String.Replace?

    Thanks jmc,

    That helped. I did have a little confusion as to how the @ character worked in this context.
    Blake

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: [RESOLVED] Issues trying to use String.Replace?

    It can be confusing to a VB developer because the only things that you need to escape there are double-quotes. It's the same principle though. If you do this in VB:
    vb.net Code:
    1. Dim text = "He said ""Hello"" to me."
    then the doubled-up double-quotes are just a representation in code. The actual String only contains one double-quote character for each pair of double-quotes that appear in the literal. The extra double-quotes in the literal are escape characters, not literal characters. If you did this:
    vb.net Code:
    1. Console.WriteLine(text)
    you would actually see:
    He said "Hello" to me.
    Likewise, C# requires you to escape a slash with another slash:, e.g.
    csharp Code:
    1. var filePath = "C:\\Folder\\Subfolder\\File.txt";
    The string doesn't actually contain pairs of slashes though. If you did this:
    csharp Code:
    1. Console.WriteLine(filePath);
    you would actually see this:
    C:\Folder\Subfolder\File.txt
    The reason that you need to escape slashes is that a slash usually denotes an escape sequence, e.g.
    csharp Code:
    1. var text = "First line.\r\nSecond line.";
    If you did this:
    csharp Code:
    1. Console.WriteLine(text);
    you would actually see this:
    First line.
    Second line.
    If there are no other escape sequences in your literal string though, it's nice not to have to double up on your slashes, especially when writing file and folder paths, which use a lot of them. A verbatim string literal means that you can do this:
    csharp Code:
    1. var filePath = @"C:\Folder\Subfolder\File.txt";
    so the code becomes easier to write and read. Each slash is then interpreted as a literal character, so no escape sequences are allowed. If you did this:
    csharp Code:
    1. var text = @"First line.\r\nSecond line.";
    2. Console.WriteLine(text);
    you would actually see:
    First line.\r\nSecond line.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [RESOLVED] Issues trying to use String.Replace?

    That's an even better explanation!

    Thank you!
    Blake

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