2012-09-16 36 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