2012-09-16 33 views
1

重定向的文件路径的过程中有没有办法在.NET类似于沙盘和DropboxPortableAHK不运行带有重定向文件路径的过程?运行在.NET

例如,如果进程想要写入文件C:\file.txt,我希望我的应用程序将该进程写入C:\Sandbox\file.txt

+0

你的应用程序是一个框架,API或类似的东西这样的过程? – Amir

回答

0

这不是简单,但基本上你需要创建本机代码(C++为例)的DLL。该DLL挂钩文件和注册表IO并将其重定向到您的沙箱目录。然后,将此DLL注入沙盒进程。该DLL将钩住此进程的活动。

这就是基本的想法。

3

由于.NET迟早将呼叫重定向到底层的操作系统,你真的可以附加一个钩子和替换的CreateFile() - 像程序来修改路径。

Detours Library可能是你所需要的。它有一些许可证的问题,尤其是在64位模式,所以看起来像这样的免费替代品:DetourXS

钩住CreateFile/WriteFile/ReadFile的确切指令在这里给出:CodingTheWheel

你只写有这样的程序,DLL:

// Our custom version of the CreateFile Windows API, having the same parameters, 
// return type, and calling convention as the version of CreateFile provided by the OS. 
HANDLE WINAPI Mine_CreateFile(LPCWSTR lpFileName,DWORD dwDesiredAccess, 
           DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecAttr, 
           DWORD dwCreateDisp, DWORD dwFlagsAttr,HANDLE hTemplate) 
{ 
    // First, call the original CreateFile provided by the operating system. 
    HANDLE hFile = Real_CreateFile(lpFileName,dwDesiredAccess,dwShareMode,lpSecAttr, 
            dwCreateDisp,dwFlagsAttr,hTemplate); 

    // Now, do whatever we want with the filename (lpFileName) 
    /// e.g., redirect C:\test.txt to C:\MyApp\test.txt 

    // Now, do whatever we want with the file handle (hFile) 

    // Call the original CreateFile 

    // Return the same value returned to us by the original API 
    return hFile; 
} 
+0

嗯,问题是关于C#而不是C++。 – Elmo

+0

不过,它可以在操作系统级而不是在.NET/CLR环境中完成。 –

+0

那么正确的答案是“不,你不能在.net中”。 – CrazyCasta