Sorry, we couldn't find your file. Was it moved, renamed or deleted?

Manoj Madushantha 1 Reputation point
2020-12-27T01:39:16.3+00:00

I recently Developed c#net Application and want to open word/ excel files in side it.

 var process = new Process();  
            string Path = Application.StartupPath + "\\Processed\\";  
            Console.WriteLine(Path+filename);  
  
            switch (ext) {   
              
                case "docx":  
                    process.StartInfo.FileName = "WINWORD.EXE";  
                    process.StartInfo.Arguments = Path + filename;  
                    process.Start();  
                    break;  
  
                case "doc":  
                    process.StartInfo.FileName = "WINWORD.EXE";  
                    process.StartInfo.Arguments = Path + filename;  
                    process.Start();  
                    break;  
  
                case "xlsx":  
                    process.StartInfo.FileName = "EXCEL.EXE";  
                    process.StartInfo.Arguments = Path + filename;  
                    process.Start();  
                    break;  
  
                case "xls":  
                    process.StartInfo.FileName = "EXCEL.EXE";  
                    process.StartInfo.Arguments = Path + filename;  
                    process.Start();  
                    break;  
  
                case "pdf":  
                    process.StartInfo.FileName = "AcroRd32.exe";  
                    process.StartInfo.Arguments = Path + filename;  
                    process.Start();  
                    break;  

But when openning word or excel file i received following message.

Sorry, we couldn't find your file. Was it moved, renamed or deleted?

51316-image-2020-12-27-074314.png

Path is displayed like this.

G:\projects\WordExcelPDFJsonApp\ImageLoadJson\bin\Debug\Processed\AL Sub Codes.docx
G:\projects\WordExcelPDFJsonApp\ImageLoadJson\bin\Debug\Processed\LV Breakers 2.doc

Any Suggestion for this problem?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,919 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
932 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bonnie DeWitt 811 Reputation points
    2020-12-27T05:48:37.133+00:00

    If you use quotes around the path and name, that will solve your problem:

    process.StartInfo.Arguments = this.AddQuotesForWhiteSpace(Path + filename);
    
    public string AddQuotesForWhiteSpace(string path)
    {
        return !string.IsNullOrWhiteSpace(path) ? 
            path.Contains(" ") && (!path.StartsWith("\"") && !path.EndsWith("\"")) ? 
                "\"" + path + "\"" : path : 
                string.Empty;
    }
    

  2. Manoj Madushantha 1 Reputation point
    2020-12-27T11:11:55.8+00:00

    Here self I wrote solution

    1. When inputting file, remove whitespaces of name and save file in Processed Folder.
    2. keep original name with file record.
    3. When viewing file on listview Show recorded (real) file name.

    It works.

    If you have better solution post it. for Education. :D

    0 comments No comments

  3. Karen Payne MVP 35,436 Reputation points
    2020-12-27T13:42:54.343+00:00

    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();  
      
      
        }  
    }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.