Home > Enterprise >  Process.Start("explorer.exe", Path) can't open the right folder
Process.Start("explorer.exe", Path) can't open the right folder

Time:02-22

As the title says, Process.Start("explorer.exe", Path) doesn't open the correct Path in explorer.

The path I expect to open is "C:/Users/%username%/Documents/My Games/FarmingSimulator2022/mods"

The path it actually opened is "This PC > Document" or can be written as "C:/Users/%username%/Documents"

BTW, I tried @Path and without "@" just Path, it's the same.

My code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace FS22_ModManagerCore
{
    public partial class MainWindow : Form
    {
        readonly string Username = Environment.UserName;
        public string GameDataPath;
        public string GameSettingXMLpath;
        public string ModFolder;
        
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void Btn_GetModPath_Click(object sender, EventArgs e)
        {
            GameDataPath = "C:/Users/"   Username   "/Documents/My Games/FarmingSimulator2022";
            GameSettingXMLpath = "C:/Users/"   Username   "/Documents/My Games/FarmingSimulator2022/gameSettings.xml";
            
            if (Directory.Exists(GameDataPath))
            {
                if (File.Exists(GameSettingXMLpath))
                {
                    ModFolder = GetModPath(GameDataPath, GameSettingXMLpath);
                }
                else
                {
                    MessageBox.Show("Game needs to run at least once! ExCode: 1002");
                }
            }
            else
            {
                MessageBox.Show("Gamedata folder doesn't exist! ExCode: 1001");
            }
        }
        
        public static string GetModPath(string GameDataPath, string XMLPath)
        {
            string ReturnPath = "";
            XmlDocument gameSetting = new();
            gameSetting.Load(XMLPath);
            XmlNode node = gameSetting.DocumentElement.SelectSingleNode("/gameSettings/modsDirectoryOverride");
            string IsActive = node.Attributes["active"].InnerText;
            if (IsActive == "false")
            {
                ReturnPath = GameDataPath   "/mods";
                MessageBox.Show(ReturnPath);    //DEBUG ONLY
                Clipboard.SetText(ReturnPath);  //DEBUG ONLY
                Process.Start("explorer.exe", ReturnPath);  //<<<<<< ISSUE !!!!!!!!
                return ReturnPath;
            }
            else
            {
                return ReturnPath;  //PLACE HOLDER
            }
            //string CusomPath = node.Attributes["directory"].InnerText;
        }
    }
}

CodePudding user response:

Windows paths use \ not /. They are often interchangable, but not always.

This

C:\>explorer.exe "C:\Users\David\Documents\My Games\FarmingSimulator2022\mods"

works. And so this does too:

System.Diagnostics.Process.Start("explorer.exe", @"""C:\Users\David\Documents\My Games\FarmingSimulator2022\mods""");
  • Related