2017-02-19 29 views
0

我有一個自定義庫在我的C#程序,它將打開一個自定義文件對話框到一個自定義位置,我所需要做的就是調用它到一個按鈕,但每次我嘗試了這個,它說Application.Run(new Form1());有一個問題,表示嘗試使用不正確的格式。這是我把代碼放在按鈕而不是openFileDialog代碼區域的情況嗎?每當我嘗試調用正常的fileDialog時,它都會運行默認的Windows版本。這裏是我的代碼:使用C調用一個按鈕的自定義庫函數

public partial class Form1 : Form 
{ 
    ALCGalleryLib.ALCGallery theGallery; 
    ALCGalleryLib.ALCGalleryFile aFile; 
    string tempFile; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void showOpenDialog_FileOk(object sender, CancelEventArgs e) 
    { 
     theGallery = new ALCGalleryLib.ALCGallery(); // this will create a new gallery object and connect to the details it already knows about (it gets them from the registry) 
     aFile = theGallery.showOpenDialog("All Files,*.*|Excel Workbooks,*.xls?"); // this call will show the gallery dialog and allow you to pick a file. it will get returned in the aFile object (or null if nothing selected) 
     if (aFile != null) 
     { 
      tempFile = aFile.saveToDisk(); // save the aFile object to disk as you will not really be able to do anything with it, and anyway, you probably do not need to do anything else with this object. this will return a temporary filename 

      // or you can choose where is gets saved with: 
      // tempFile=aFile.saveToDisk("some filename.xlsx"); 

      // or assign your filename to tempFile and then... 
      // aFile.saveToDisk(tempFile); 

      // either of the above calls will save the file from the gallery to disk and return the filename in tempFile 
     } 
     else 
     { 
      // nothing was selected 
     } 
    } 

    private void openFile_Click(object sender, EventArgs e) 
    { 
     theGallery.showOpenDialog("All Files,*.*|Excel Workbooks,*.xls?"); 
    } 
} 
+0

看起來像一個32位/ 64位問題。看到這個問題:http://stackoverflow.com/questions/2023766/an-attempt-was-made-to-load-a-program-with-an-incorrect-format-even-when-the-p – HHLV

回答

0

如果你有一個Windows 64-bit操作系統,它是您的應用程序ALCGalleryLib之間有衝突,一個是32位,另一種是64位

如果ALCGalleryLib位於32位,請參閱Project Properies,Build Tab,Platform Target必須爲x86,而不是任何CPU或x64。

如果ALCGalleryLib位於64位,請參閱Project Properies,Build Tab,Platform Target必須是x64或任何CPU。

+0

謝謝!這很有效,事實證明庫運行的是64位,但不是運行「任何CPU」,而是要求強制進入「x64」模式。 –