简体   繁体   English

路径中的C#反斜杠

[英]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). 我正在使用Windows 10(1805),我的客户使用Windows Server 2016(标准版)。

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. IMGPath是通过正确传输的Windows FileDialog Box给出的,但是我无法在客户计算机上调试。

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. 但是奇怪的是,在我自己的Machine Win 10上,它没有任何负面影响,图像保存在正确的文件夹中。

Question: Is it possible that this may lead in Windows Server 2016 to problems? 问题:这可能会导致Windows Server 2016出现问题吗?

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. Windows 10会自动更正此问题,但Windows Server 2016不会。

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

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

You can use Path.Combine method . 您可以使用Path.Combine方法。 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 DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")可能正在使用客户端DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") /时区

Please try using var date1 = DateTime.Now.ToString(CultureInfo.InvariantCulture); 请尝试使用var date1 = DateTime.Now.ToString(CultureInfo.InvariantCulture); and be aware of TimeZone issues. 并注意TimeZone问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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