执行长文件路径的文件
我正在尝试编写一个程序来传递一个文件名字符串。 然后我希望程序启动/打开我作为参数传递的文件。
我做了一些研究,我很确定我必须使用这样的东西: 链接
但我只找到了打开的示例(为了写入)文件、删除和查找文件。MS Library
我在调整代码时遇到了麻烦。
谁能帮助我吗? 这是我想到的:
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;
}
}
}
编辑: 对于我不想要什么似乎有些困惑,所以我会尽力澄清。
我有一个 FoxPro 应用程序,其中存储了记录。对于其中一些记录,我想关联图像或文档,因此我将其路径存储到数据库中的字段中。 到目前为止,一切都很好。 问题在于文件大小达到了几个 TB(即 Tera Bytes),并且路径比 Windows API 允许的最大长度要长得多。
我想直接从 Fox 打开这些文件,但 Fox 不支持长路径。 所以我想用 C# 编写一个应用程序,将长文件名作为参数传递,并由该应用程序打开它...
问题是 C# 也“继承”了 Windows API 的限制。 我遇到了一种解决方法,用于删除、移动和打开(在编辑模式下)具有如此长路径的文件。但我想要的只是让 Windows 打开文件并将其显示给用户。
希望我说清楚了。 抱歉英语不好。
I'm trying to write a program to wich pass a string that is a filename.
I then want the program to start/open the file i pass as a parameter.
I've done some research and i'm pretty sure i have to use something like this:
Link
But i've only found examples to Open (in order to wirte) files, delete, and find files.MS Library
I'm having trouble in adapting the code.
Can anyone help me?
Here's what i came up with:
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;
}
}
}
EDIT:
There seems to be some confusion as to what i wan't, so i'll try to clarify.
I have a FoxPro app in which i have records stored. For some of these records i want to associate an image or a document, so i store it's path into a field in the database.
So far, so good.
The problem is that the files ascend to several TB (that's right Tera Bytes) and the paths are way longer than the maximum allowed by the windows APIs.
I want to open these files directly from Fox but Fox does not support the long paths.
So i want to write an app in C# that i pass the long file name as a parameter and have it opened by that app...
The problem is that C# also 'inherits' the Windows APIs' limitation.
I've come across a workaround for deleting, moving and opening (in edit mode) files with such longpaths. But what i want is just have windows open the file and show it to the user.
Hope i made myself clear.
Sorry for bad english.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为使用 FileStream 类可以做到这一点。或者我可能误解了你的问题?
I think this is possible using FileStream class. Or may be i misunderstood you question please?
事实证明我的代码几乎正确:
这是正确的代码:(如果有人想知道)
It turns out my code was almost correct:
Here's the correct code: (if anyone wants to know)