2011-01-14 75 views
0

我的程序需要讀取和寫入其他機器上可能位於另一個域中的文件夾。所以我使用System.Runtime.InteropServices來添加共享文件夾。當它在我的Windows服務的主菜單中被硬編碼時,它工作得很好。但是從那以後出了點問題,我不知道它是編碼錯誤還是配置錯誤。如何查看我的程序可以訪問哪些共享文件夾?

  • 共享文件夾的範圍是什麼?如果我的程序中有一個線程添加共享文件夾,整個本地機器能否看到它?
  • 有沒有辦法查看共享文件夾已被添加?或者有沒有辦法查看何時添加文件夾?

    [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
    internal static extern System.UInt32 NetUseAdd(string UncServerName, int Level, ref USE_INFO_2 Buf, out uint ParmError); 
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
    internal struct USE_INFO_2 
    { 
        internal LPWSTR ui2_local; 
        internal LPWSTR ui2_remote; 
        internal LPWSTR ui2_password; 
        internal DWORD ui2_status; 
        internal DWORD ui2_asg_type; 
        internal DWORD ui2_refcount; 
        internal DWORD ui2_usecount; 
        internal LPWSTR ui2_username; 
        internal LPWSTR ui2_domainname; 
    } 
    
    private void AddSharedFolder(string name, string domain, string username, string password) 
    { 
        if (name == null || domain == null || username == null || password == null) 
         return; 
    
        USE_INFO_2 useInfo = new USE_INFO_2(); 
        useInfo.ui2_remote = name; 
        useInfo.ui2_password = password; 
        useInfo.ui2_asg_type = 0; //disk drive 
        useInfo.ui2_usecount = 1; 
        useInfo.ui2_username = username; 
        useInfo.ui2_domainname = domain; 
        uint paramErrorIndex; 
        uint returnCode = NetUseAdd(String.Empty, 2, ref useInfo, out paramErrorIndex); 
        if (returnCode != 0) 
        { 
         throw new Win32Exception((int)returnCode); 
        } 
    } 
    

回答

1

在計算機中的每個線程在一個特定的用戶帳戶運行。共享文件夾具有安全設置,即它們受到基於ACL的訪問控制,因此某些用戶可能具有訪問權限,而其他用戶可能不具有訪問權限。這意味着程序中的線程可能能夠「看到」某些共享文件夾,而同一臺計算機中的其他線程(包括使用桌面的交互式用戶)可能無法「看見」這些文件夾。

總結:你應該承擔很少。

+0

謝謝。我確實解決了這個問題,這似乎是程序沒有正確讀取配置文件的一部分。 – 2011-01-14 13:04:44

相關問題