2017-06-29 221 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就足够了,也许这就够了。

相关问题