无论如何要读写.png文件和链接.txt文件名为.png文件相同吗?

发布于 2025-01-28 15:00:20 字数 1438 浏览 3 评论 0原文

它最多以窗户形式制成

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2025-02-04 15:00:21

谢谢大家。我已经找到了解决方案并替换了:

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);*/
            }
        }

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);*/
            }
        }
岛歌少女 2025-02-04 15:00:20

如果我正确理解,则可以使用MultiSelect OpenFileDialog的属性。这样,您可以打开多个文件(在您的情况下,具有不同扩展名的同一文件名)并将其显示给用户。

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
    }
}

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文