简体   繁体   中英

C# backslashes in path

I have currently a bug in my software which is only reproducible on customer side. I am using Windows 10 (1805) and my customer uses Windows Server 2016 (Standard).

The problem is that when you set the image path with dialog box it seems that it is saved in a wrong format, but I am not sure.

// Snap (capture) an image to the memory
string path = IMGPath + "\\" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".jpeg";

IMGPath is given over the Windows FileDialog Box which is transfered correctly, but I cant debug on the customers computer.

Before the code above I have used "/" which may lead to the effect that image saving function couldnt find the correct path. But the strange thing is that on my own Machine Win 10 it doesnt have any negative effect, the images were saved in the correct folder.

Question: Is it possible that this may lead in Windows Server 2016 to problems?

Thanks in advance:)

Update: Finally it worked, but the problem was that I have mixed up slashes and backslashes. Windows 10 corrects this automatically, but Windows Server 2016 not.

如果您使用Path.Combine而不是执行自己的字符串连接,那么这将减少发生错误的可能性。

var path = Path.Combine(IMGPath, $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")}.jpeg");

You can use Path.Combine method . which combines array of strings into a path.

string[] paths = {IMGPath, "\\", DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss"), ".jpeg"};
string fullPath = Path.Combine(paths);
Console.WriteLine(fullPath);

DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") may be using the Clients Culture/TimeZone

Please try using var date1 = DateTime.Now.ToString(CultureInfo.InvariantCulture); and be aware of TimeZone issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM