2016-08-26 23 views
0

ボタンをクリックしてデスクトップに.websiteショートカット(ピン設定可能なショートカット)を作成しようとしています(C#)。デスクトップにピンナブルなショートカットを作成するC# - .website extension

[InternetShortcut] 
URL=https://Asite.org 

もし私がメモ帳で開いたときに作成されたファイルは、次のようになります - (が閉じる直前しかし、ブラウザが開き、作品)

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 

namespace ShortcutUpdater 
{ 
    public partial class UserControl1 : UserControl 
    { 

    private void urlShortcutToDesktop(string linkName, string linkUrl) 
    { 
     string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); 

     using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".website")) 
     { 
      writer.WriteLine("[InternetShortcut]"); 
      writer.WriteLine("URL=" + linkUrl); 
      writer.Flush(); 
     } 
    } 


     public UserControl1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string linkName = "My Site"; 
      string linkUrl = "https://Asite.org"; 
      urlShortcutToDesktop(linkName, linkUrl); 
     } 

私は既存のコードを持っていますブラウザからそのアイコンをデスクトップにドラッグしてメモ帳で開くと、これでぴったりのショートカットが作成されます(これは私が達成したいと思いますので、ブラウザを閉じてブラウザを閉じるだけです):

[{000214A0-0000-0000-C000-000000000046}] 
Prop4=31,asite.org 
Prop3=19,11 
[{A7AF692E-098D-4C08-A225-D433CA835ED0}] 
Prop5=3,0 
Prop9=19,0 
[InternetShortcut] 
URL=https://asite.org/ 
IDList= 
IconFile=https://asite.org/favicon.ico 
IconIndex=1 
[{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}] 
Prop5=8,Microsoft.Website.50DFA192.38C0BBDC 

これを行うにはどのような方法が最適ですか?

+0

これが発生する原因となるコードではない可能性があります。それはあなたのコンピュータ(すなわちインターネット)上の特定のスレッドまたは機能にアクセスするためのアプリケーションを禁止するWindowsセキュリティかもしれません – jdave

+0

したがって、私は "Prop4 = blah blah、Prop2 = blah blah"のものを再作成しようとすべきではありませんか? 私はそれらが何であるか、それらにアクセス/作成する方法がわかりません。 – Hanny

+0

ピン設定のショートカットを右クリックしようとしましたが、アクセスプロパティが表示されましたか?多分管理者が必要でしょうか? – jdave

答えて

0

実際にinternet shortcutsを作成するための公式プログラミングインターフェイスがあります。残念ながら、.NETプログラムではインターフェイスをすぐに利用できず、Tlbimp.exe (Type Library Importer)で使用するタイプライブラリもありません。それには2つのオプションしかありません:C#でインターフェイス定義を複製しようとしているか、ネイティブ実装を使用しています。

私はすぐに.NETコードによってを消費することができ、特に、C++/CLI クラスライブラリ、ネイティブ実装と一緒に行く提案します。実装は、ネイティブ実装クラス(InternetShortcutImpl)と、管理クラスに機能を公開するためのプロキシ(refクラス(InternetShortcut))の2種類から構成されています。両方のクラスを同じファイルに配置することができます。

この

は、ネイティブアクセスクラスの実装です:

#pragma once 

#include <comip.h> 
#include <comdef.h> 
#include <IntShCut.h> 
#include <ShObjIdl.h> 
#include <string> 

using namespace System::Runtime::InteropServices; 

_COM_SMARTPTR_TYPEDEF(IUniformResourceLocatorW, __uuidof(IUniformResourceLocatorW)); 
_COM_SMARTPTR_TYPEDEF(IPersistFile, __uuidof(IPersistFile)); 
_COM_SMARTPTR_TYPEDEF(IShellLinkW, __uuidof(IShellLinkW)); 
class InternetShortcutImpl 
{ 
public: 
    InternetShortcutImpl() 
    { 
     HRESULT hr = ::CoCreateInstance(CLSID_InternetShortcut, nullptr, 
             CLSCTX_INPROC_SERVER, 
             IID_IUniformResourceLocatorW, 
             (void**)&m_spURL); 
     if (FAILED(hr)) { 
      throw gcnew ExternalException(nullptr, hr); 
     } 
    } 

    void SetURL(const std::wstring& url) { 
     HRESULT hr = m_spURL->SetURL(url.c_str(), 0x0); 
     if (FAILED(hr)) { 
      throw gcnew ExternalException(nullptr, hr); 
     } 
    } 

    void SetIconLocation(const std::wstring& iconpath, int index) { 
     IShellLinkWPtr spLink; 
     HRESULT hr = m_spURL->QueryInterface(IID_PPV_ARGS(&spLink)); 
     if (SUCCEEDED(hr)) { 
      hr = spLink->SetIconLocation(iconpath.c_str(), index); 
     } 
     if (FAILED(hr)) { 
      throw gcnew ExternalException(nullptr, hr); 
     } 
    } 

    void Save(const std::wstring& pathname, bool remember) { 
     IPersistFilePtr spFile; 
     HRESULT hr = m_spURL->QueryInterface(IID_PPV_ARGS(&spFile)); 
     if SUCCEEDED(hr) { 
      hr = spFile->Save(pathname.c_str(), remember ? TRUE : FALSE); 
     } 
     if (FAILED(hr)) { 
      throw gcnew ExternalException(nullptr, hr); 
     } 
    } 
private: 
    IUniformResourceLocatorWPtr m_spURL; 
}; 

そして、これは、本質的にのみ転送し、それがメンバーとして含まれているネイティブクラスに.NETから呼び出すプロキシクラス、次のとおりです。

#include <msclr\marshal_cppstd.h> 

using namespace System; 

namespace ShortcutInteropLib { 
#pragma warning(push) 
#pragma warning(disable : 4461) // "this class has a finalizer but no destructor" 
    public ref class InternetShortcut 
    { 
    public: 
     InternetShortcut() { 
      m_pImpl = new InternetShortcutImpl(); 
     } 

     void SetURL(String^ url) { 
      m_pImpl->SetURL(msclr::interop::marshal_as<std::wstring>(url)); 
     } 

     void SetIconLocation(String^ iconpath, int index) { 
      m_pImpl->SetIconLocation(msclr::interop::marshal_as<std::wstring>(iconpath), 
             index); 
     } 

     void Save(String^ pathname, bool remember) { 
      m_pImpl->Save(msclr::interop::marshal_as<std::wstring>(pathname), 
          remember); 
     } 

     !InternetShortcut() { 
      delete m_pImpl; 
     } 

    private: 
     InternetShortcutImpl* m_pImpl; 
    }; 
#pragma warning(pop) 
} 

これは、.NETアセンブリから参照できるライブラリ全体を形成します。呼び出し元のコードは自明簡単です:

class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     var shortcut = new InternetShortcut(); 
     shortcut.SetURL(@"http://www.example.com"); 
     //shortcut.SetIconLocation(@"http://www.example.com/favicon", 1); 
     shortcut.Save(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
         + @"\MyShortCut.url", false); 
    } 
} 

これは、デスクトップ上のhttp://www.example.comへのショートカットを作成します。 SetIconLocationを実装している間は、URLでどのように使用するかわかりません。アイコンインデックスは、結果のMyShortCut.urlファイルに適切に格納されていますが、パスはローカルファイルシステムへのパスとして解釈されます。

コードは私のシステム上で次のインターネットショートカットを作成する:

[{000214A0-0000-0000-C000-000000000046}] 
Prop3=19,2 
[InternetShortcut] 
IDList= 
URL=http://www.example.com/ 


C++/CLIクラスライブラリは、ネイティブコードが含まれているので、あなたが選択する必要がありますターゲットアーキテクチャ。クライアントコードは適切なアセンブリを参照する必要があります。

私はURLを渡すことができます特別な構文があるかどうか分からない、とシステムがローカルファイルシステムのパスとして引数を解釈する必要はありません。もし誰かが知っていれば、私はそれについて聞いてうれしいです。

関連する問題