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.