Hello @Manoj Madushantha
Another way to go is use Path.Combine rather than + when combining path and filename as Path.Combine places \ character where needed. And on Windows 10 I've never seen an issue going this was with spaces in folder and/or file name. Note in the two code samples below you can write less code to achieve the same as what you had.
Alternate to Application.StartupPath
private static string _basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Processed");
C# 9 example
public class CodeSample
{
private static string _basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Processed");
public static void Demo(string filename, string extention)
{
var process = new Process();
process.StartInfo.FileName = extention switch
{
"docx" => "WINWORD.EXE",
"doc" => "WINWORD.EXE",
"xlsx" => "EXCEL.EXE",
"xls" => "EXCEL.EXE",
"pdf" => "AcroRd32.exe",
_ => throw new NotImplementedException("Unknown file type")
};
if (!File.Exists(Path.Combine(_basePath, filename))) return;
process.StartInfo.Arguments = Path.Combine(_basePath, filename);
process.Start();
}
}
Prior to C# 9
public class CodeSample
{
private static string _basePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Processed");
public static void Demo(string filename, string extention)
{
var process = new Process();
switch (extention)
{
case "docx":
case "doc":
process.StartInfo.FileName = "WINWORD.EXE";
break;
case "xlsx":
case "xls":
process.StartInfo.FileName = "EXCEL.EXE";
break;
case "pdf":
process.StartInfo.FileName = "AcroRd32.exe";
break;
default:
throw new NotImplementedException("Unknown file type");
}
if (!File.Exists(Path.Combine(_basePath, filename))) return;
process.StartInfo.Arguments = Path.Combine(_basePath, filename);
process.Start();
}
}