greyBow
greyBow

Reputation: 1348

Windows file path in string getting confused for escape characters

How can I save to a Windows path without Unity thinking the \ is an escape character? I'm getting errors like this: Assets/_Scripts/CaptureSaveScreenshot.cs(50,93): error CS1009: Unrecognized escape sequence `\k'

public void GrabIt(string capturePath)
{   
    string dtString = System.DateTime.Now.ToString("yyyyMMddHHmmssfff");

    if(width > 0 && height > 0)
    {
        if (Application.platform == RuntimePlatform.WindowsWebPlayer)
            snapShot.CaptureAndSaveAtPath(x, y, width, height, "C:\Users\kenmarold\Screenshots\screenshot_"+dtString+".png");   // Save to Windows

        if (Application.platform == RuntimePlatform.OSXWebPlayer)
            snapShot.CaptureAndSaveAtPath(x, y, width, height, "/Users/kenmarold/Screenshots/screenshot_"+dtString+".png");     // Save to Mac

Upvotes: 0

Views: 2779

Answers (2)

user585968
user585968

Reputation:

Use the @ symbol before the string like so:

 snapShot.CaptureAndSaveAtPath(x, y, width, height, 
             @"C:\Users\kenmarold\Screenshots\screenshot_"+
             dtString + ".png");   // Save to Windows

That tells the compiler to treat everything in the string as literal and doesn't require you to escape everything. This is great when using file paths.

Upvotes: 1

Umair M
Umair M

Reputation: 10720

Use \\ in path string so it would consider it as \.

Like

"C:\\Users\\kenmarold\\Screenshots\\screenshot_"+dtString+".png"

Learn more about Escape Sequences in C#.

OR you can use Verbatim String instead of Escape Sequence.

And Thumb Up if its helpful.

Upvotes: 2

Related Questions