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
    {
        [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 字节),并且路径比 Windows API 允许的最大值长得多。

我想直接从 Fox 中打开这些文件,但 Fox 不支持长路径。所以我想用 C# 编写一个应用程序,我将长文件名作为参数传递并让它由该应用程序打开......

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

希望我说清楚了。抱歉英语不好。

4

2 回答 2

0

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

于 2012-02-13T12:31:50.987 回答
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;
        }
    }
}
于 2012-02-14T14:20:17.220 回答