2017-06-29 226 views
0

我需要以編程方式從Visio中的Sharepoint打開文檔。但是,當我瀏覽到網絡文件夾,選擇一個文件並點擊打開,我 得到以下錯誤:C#OpenFileDialog:文件名,目錄名稱或卷標語法不正確

The filename, directory name, or volume label syntax is incorrect enter image description here

當錯誤搜索,我發現了以下文件:https://msdn.microsoft.com/en-us/library/ms832054.aspx。所以我猜想文件名稱包含非法字符。我嘗試使用FileOk事件覆蓋文件名的驗證:

public void openFile() { 
    OpenFileDialog sf = new OpenFileDialog(); 
    sf.FileOk += openFileDialog_FileOk; 
    if (sf.ShowDialog() == DialogResult.OK) 
    { 
     var app =(Microsoft.Office.Interop.Visio.Application)context.Application; 
     app.Documents.Open(sf.FileName); 
    } 
} 

private void openFileDialog_FileOk(object sender, CancelEventArgs e) 
{ 
    var sfd = sender as OpenFileDialog; 
    var file = new FileInfo(sfd.FileName); 
    if (file.Name.Contains('#')) 
     e.Cancel = true; 
} 

但事件不會觸發。使用標準的Visio界面可以從Sharepoint打開文件,但文件對話框看起來有點不同: enter image description here

我怎樣才能獲得類似的文件對話框?所以我的問題是:我如何以編程方式從Sharepoint(網絡文件夾)打開Visio文檔?

+1

'發件人爲SaveFileDialog'?不是'OpenFileDialog'? – apocalypse

回答

1

由於Visio不提供app.GetOpenFilename API,因此您運氣不好。但你可以使用另一個辦公應用程序。如Excel,例如:

var excel = new Excel.Application(); 
var fileName = excel.GetOpenFilename(); 
excel.Quit(); 

var visio = new Visio.Application(); 
visio.Documents.Open(fileName); 

它提供了一個「類似的對話」和「正常的URL」,即由Visio API沒有任何問題的理解。

問題可能是Visio API不能理解帶有@SSL部分的UNC DAV文件路徑格式,這是由默認的「內置」OpenFileDialog(或可能是別的)提供的。檢查默認對話框返回的.FileName的值是多少。順便說一句,爲了防止錯誤信息,設置sf.CheckFileExists = false就足夠了,也許這就夠了。

相關問題