Home > Blockchain >  How to display if file path is right? Image or not image
How to display if file path is right? Image or not image

Time:10-14

I can't figure out how to get the cmd to display whether the user has input the correct file path for an image. I know I have to console.??? but I'm not sure how to write it out.

Set up:

  1. User inputs their file path
  2. CMD takes that info and tells them whether it's a image file (0xFFD8 file) or not
  3. CMD then creates a .CSV file containing the file path, file type, and MD5 hash

I'm stuck at getting the cmd to tell the user whether it's a jpg file or not.

Any idea where I'm going wrong in my code?

internal class Program
{
    
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter your file path location:");
        string mainFile = MainFileInput();


        Console.WriteLine("Let's determine what type of file this is:");
        string typeFile = ImageType();
    }

    public static string MainFileInput()
    {
        string mainFile = Console.ReadLine();
        while (File.Exists(mainFile) == false)
        {
            Console.WriteLine("Main file does not exist. Please enter another file: ");
            mainFile = Console.ReadLine();
        }
        Console.WriteLine("File exists.");
        return mainFile;
    }

    public static ImageType HeaderType(string typefile)
    {
        string checkType;
        byte[] headerBytes;
        using (FileStream fileStream = new FileStream(typefile, FileMode.Open))
        {
            const int mostBytesNeeded = 11;//For JPEG

            if (fileStream.Length < mostBytesNeeded)
                return ImageType.Unknown;

            headerBytes = new byte[mostBytesNeeded];
            fileStream.Read(headerBytes, 0, mostBytesNeeded);
        }
        if (headerBytes[0] == 0xFF &&//FF D8)
        {
            return ImageType.JPEG;
        }
        return ImageType.Unknown;
    }
    public enum ImageType
    {
        Unknown,
        JPEG,
    }
}

CodePudding user response:

You need to call the method HeaderType(...) from Main with your file name (mainFile - after you received it from calling the method MainFileInput(...)).

The return value from HeaderType(...) will be of type ImageType.
You can print it after converting to string using .ToString() method.

So your complete Main will be something like:

static void Main(string[] args)
{
    Console.WriteLine("Please enter your file path location:");
    string mainFile = MainFileInput();

    Console.WriteLine("Let's determine what type of file this is:");
    ImageType theType = HeaderType(mainFile);
    Console.WriteLine("Image type: "   theType.ToString());
}

CodePudding user response:

Use C# to determine whether the file is an image.

  static void Main(string[] args)
 {
       Console.WriteLine("Please enter your file path location:");
       string mainFile = MainFileInput();

       Console.WriteLine("Let's determine what type of file this is:");
       boolean ispic = IsPicture(mainFile);
       if(ispic == true)
      { 
          Console.WriteLine("The file is a jpg image");
      }
 }

   public static string MainFileInput()
{
    string mainFile = Console.ReadLine();
    while (File.Exists(mainFile) == false)
    {
        Console.WriteLine("Main file does not exist. Please enter another file: ");
        mainFile = Console.ReadLine();
    }
    Console.WriteLine("File exists.");
    return mainFile;
}

 private static bool IsPicture(string filePath)//filePath is the full path of the file
    {
        try
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(fs);
            string fileClass;
            byte buffer;
            byte[] b=new byte[2];
            buffer = reader.ReadByte();
            b[0] = buffer;
            fileClass = buffer.ToString();
            buffer = reader.ReadByte();
            b[1]=buffer;
            fileClass  = buffer.ToString();


            reader.Close();
            fs.Close();
            if (fileClass == "255216 ")//255216 is jpg; 7173 is gif; 6677 is BMP, 13780 is PNG; 7790 is exe, 8297 is rar
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
  • Related