2012-02-13 140 views
0

我试图写一个程序至极传递一个字符串,是一个文件名执行文件。 然后我想让程序启动/打开我作为参数传递的文件。长文件路径

我做了一些研究,我敢肯定,我不得不使用这样的事情: Link

但我只找到实例打开(以WIRTE)文件,删除和找到文件。​​

我在适应代码的麻烦。

任何人都可以帮助我吗? 这就是我想出了:

using System; 
using System.Diagnostics; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.ComponentModel; 
using System.Runtime.InteropServices; 
using Microsoft.Win32.SafeHandles; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
     static extern bool abreFicheiro(string lpFileName, bool bFailIfExists); 

     static void Main(string[] args) { 
      string caminho = fixPathForLong(@args[0]); 
      abreFicheiro(caminho); 
     } 

     public static bool abreFicheiro(string caminho) { 
      Process.Start(caminho); 
      if (!abreFicheiro(caminho, false)) 
      { 
       throw new Win32Exception(); 
      } 

      return true; 
     } 

     private static string fixPathForLong(String path) 
     { 
      if (!path.StartsWith(@"\\?\")) 
       path = @"\\?\" + path; 
      return path; 
     } 
    } 
} 

编辑: 似乎有一些混乱,我wan't,所以我会尽力澄清。

我有一个FoxPro应用程序在我所存储的记录。对于我想要关联图像或文档的这些记录中的一部分,我将它存储到数据库中的字段中。 到目前为止,这么好。 问题是文件上升到几个TB(这是正确的Tera字节),并且路径长于窗口API允许的最大长度。

我想直接从福克斯福克斯,但打开这些文件不支持长路径。 所以我想在C#中编写一个应用程序,我通过长文件名作为参数,并通过该应用程序打开它...

问题是,C#还'继承'的Windows APIs的限制。 我遇到了一个解决方法,用于删除,移动和打开(在编辑模式下)具有此类长路径的文件。但是我想要的只是打开文件并将其显示给用户。

希望我自己清楚。 对不起,英语不好。

+0

认沽C#作为您的问题标签,请 – m0skit0 2012-02-13 12:17:13

+0

完成。感谢提醒。 – user1206709 2012-02-13 12:27:29

+0

它不清楚你想问什么。如果你想减少漫长的路径,你可以使用GetShortPathName API http://pinvoke.net/default.aspx/kernel32/GetShortPathName.html – Mohit 2012-02-13 12:37:51

回答

0

我认为这是可能使用FileStream类。或者可能是我误解了你的问题吗?

+0

FileStream是打开文件,所以我可以编辑它的内容(我认为)。我想要的是通过一个单词或PDF或图像或任何其他路径,并有窗户打开该文件在它的默认查看器/编辑器。的Process.Start();这样做,但具有MAX_PATH限制。 – user1206709 2012-02-13 14:08:07

0

原来我的代码是几乎正确:

下面是正确的代码:(如果有人想知道)

using System; 
using System.Diagnostics; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.ComponentModel; 
using System.Runtime.InteropServices; 
using Microsoft.Win32.SafeHandles; 

namespace ConsoleApplication1 
{ 
    class Program { 
     static void Main(string[] args) 
     { 
      string caminho = fixPathForLong(@args[0]); 
      Process.Start(caminho); 
     } 

     private static string fixPathForLong(String path) { 
      if (!path.StartsWith(@"\\?\")) 
       path = @"\\?\" + path; 
      return path; 
     } 
    } 
}