2012-02-13 116 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; 
     } 
    } 
}