Home > Blockchain >  Anyway to read and write .png file and link .txt file named the same as .png file?
Anyway to read and write .png file and link .txt file named the same as .png file?

Time:05-13

Its made in Windows Forms at the most, in a button

 public Photo(string filename)
            {
                bitmap = new Bitmap(filename); /*shows the picture.png */
                CreationDate = File.GetCreationTime(filename).ToString(); /*shows creationtime of picture*/
                Filelink = filename; /*shows filename opened*/
                FileName = Path.GetFileName(filename); /*shows opened file path*/
                /*Description = FileMode.Open.ToString(filename);*/


 private void Button1_Click(object sender, EventArgs e)
            {
    
                OpenFileDialog ofd = new OpenFileDialog
                {
                    Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"
                };
    
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    
                    try
                    {
                        Photo photoph = new Photo(ofd.FileName);
                        /*dataGridView1.Rows.Add(photoph.bitmap, photoph.FileName, photoph.Filelink, photoph.CreationDate);*/
                        listing.Add(ofd.FileName, dataGridView1);
                    }
                    catch
                    {
                        MessageBox.Show("Impossible to open file. Choose another one", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
    

CodePudding user response:

If I understand correctly, you can use Multiselect property of OpenFileDialog. In this way you can open multiple file (in your case same file name with different extensions) and show them to users.

OpenFileDialog ofd = new OpenFileDialog{ Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"};

DialogResult dr = ofd.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
    // Read the files
    foreach (var file in ofd.FileNames) 
    {
       // Do your things
    }
}

CodePudding user response:

Thanks everyone. I already found solution and made it with Replace:

public Photo(string filename)
        {
            string s = "";
            s = filename.Remove(filename.Length-3, 3);
            s  = "txt";
            FileStream fsIn = new FileStream(s, FileMode.Open,
           FileAccess.Read, FileShare.Read);
            using (StreamReader sr = new StreamReader(fsIn))
            {
                string input;
                while (sr.Peek() > -1)
                {
                    input = sr.ReadLine();
                    Description  = input;
                }
                bitmap = new Bitmap(filename);
                CreationDate = File.GetCreationTime(filename).ToString();
                Filelink = filename;
                FileName = Path.GetFileName(filename);
                Description = File.ReadAllText(s);/*FileMode.Open.ToString(filename);*/
            }
        }
  • Related