How to change seperator in path of ini?

let paths: Vec<_> = cdirs().iter().map(|d| PathBuf::from(d).join("config.ini")).collect();

Debug codes within vscode in win10 system, I expected to get: "C:\Users\dawns\AppData\Roaming{my application}\config.ini", but paths was"C:\Users\dawns\AppData\Roaming/{my application}\config.ini", the seperator before {my application} is "/" not "\".
it make mistake when open ini file.
how to correct it?

I have found Windows to be very forgiving regarding the path separator. You may have to provide more details, like the exact error message or a bit more code, to get help.


The error message:
Error: Io(Os { code: 5, kind: PermissionDenied, message: "拒绝访问。" })
And I am sure the directory has total right to visit the C:\Users\dawns\AppData\Roaming/{my application}, including write, modify an others for all users.

Huh. I'll try to reproduce that.

Does the {my application} directory already exist?

use std::fs::File;
use std::path::Path;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // C:\Users\dawns\AppData\Roaming/{my application}\config.ini

    let path = Path::new(r"C:\Users\Coding-Badly\AppData\Roaming/{my application}\config.ini");
    println!("path = {}", path.display());
    match File::create(&path) {
        Ok(file) => println!("created"),
        Err(error) => eprintln!("error = {}", error),


path = C:\Users\Coding-Badly\AppData\Roaming/{my application}\config.ini

Seems to work fine here. Windows 7.

Are you perhaps not using a raw string / not escaping the backslashes?

Thanks your help.
Your code tell me \ or / is not the reason of mistake.
I'll check it.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.