向收藏夹中添加网址

Posted on 2019-01-31 21:59  努力成长静待花开  阅读(255)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  Environment类的GetFolderPat方法

  StreamWriter类的WriterLine方法

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string favoriterFolder=Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
                StreamWriter sw = File.CreateText(favoriterFolder+"\\"+textBox2.Text+textBox1.Text+".url");
                sw.WriteLine("[InternetShortcut]");
                sw.WriteLine("URL="+textBox1.Text);
                sw.Close();
                MessageBox.Show("添加成功!");
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }