Search code examples
c#driverp2

How to invoke P2 card formatter (P2FormatApp) and send format command using c#


I currently have a P2 SD card that I want to format. But I want to use the P2FormatApp.exe to format, all that while using a c# program.

The manual way of formatting the sd card using said app is by starting explorer, then right click on the drive, select format and then only the format window of said app will start.

the format app window

I would like to know if there is a way to achieve the same result by using c#. I have tried starting the app by using the ProcessStartInfo class but to no avail.

My motive of using said app is so that the SD card volume label can be rewritten by the sd card serial number. and I believe I can achieve this easily by using the app above.

Any insights regarding this problem is greatly appreciated!

Here is my code snippet of my take on invoking the P2FormatApp.exe app. I tried changing the drive directory to the SD card then invoking the app but it says that the directory couldnt be detected.

            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "P2FormatApp.exe";
                startInfo.Arguments = "/fs:FAT /v:MyVolume /q " + "E";
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardInput = true;
                // tried changing the working directory to the app location path also didnt resolve the issue
                //startInfo.WorkingDirectory = @"C:\Windows\SysWOW64";
                Process p = Process.Start(startInfo);

                StreamWriter processInputStream = p.StandardInput;
                processInputStream.Write("\r\n");
                p.WaitForExit();

            } catch (Exception ex) {
                
                Console.WriteLine(ex.Message);
            }

the error message that i got :

An error occurred trying to start process 'P2FormatApp.exe' with working directory 'E:'. 指定されたファイルが見つかりません。

the japanese text translates to : The specified file cannot be found.


Solution

  • As @Hans Passant mentioned in the comments, use the full path of the program / application being invoked, i.e.:

    using System;
    using System.Diagnostics;
    using System.IO;
    
        // FULL path to application file is required ...
    
        const String k_app_file = @"C:\Windows\SysWOW64\P2FormatApp.exe";
        
        if ( System.IO.File.Exists( k_app_file ) ) {
        
            try
            {
                
                ProcessStartInfo startInfo = new ProcessStartInfo();
    
                startInfo.FileName = k_app_file;
                startInfo.Arguments = "/fs:FAT /v:MyVolume /q " + "E";
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardInput = true;
    
                Process p = Process.Start( startInfo );
    
                StreamWriter processInputStream = p.StandardInput;
                
                processInputStream.Write( "\r\n" );
                
                p.WaitForExit();
    
            } 
            catch ( Exception ex ) {
                
                Console.WriteLine( ex.Message );
    
            }
            
        }
        else {
        
            // Required application is missing ...
    
            Console.WriteLine( $@"* Required application file ({k_app_file}) does not exist" );
            
        }