Проверка на lnk в try catch - C#
Формулировка задачи:
нужно чтобы в блоке try catch была проверка на то, что выбранный файл ярлык и выбрасывало сообщения.
if (open.ShowDialog() == DialogResult.OK)
{
try
{
if (open.FileName == open.FileName + ".link")
{
throw (new Exception("The file don't exist!"));
}
// missing
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
// missing
}
pictureBox10.ImageLocation = open.FileName;
}Решение задачи: «Проверка на lnk в try catch»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.DereferenceLinks = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
try
{
MessageBox.Show(IsLnkFilename(openFileDialog1.FileName).ToString());
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
private bool IsLnkFilename(string _filename) {
if (_filename.EndsWith(".lnk")) return true;
throw new Exception("not lnk!");
}
}
}