小女子第一回写服务的程序,请大家帮忙

小女子第一次写服务的程序,请大家帮忙
为什么服务启动无法访问到服务器的共享文件夹?访问本地的文件夹就可以

------解决方案--------------------
我不知道具体应该如何写代码,但我知道你这样肯定是访问不了SERVER的.一般通过WINDOWS访问域上的机器都会自动加入你的用户名和密码.你可以想法试试能不能把用户信息加进去.下边是格式
UserName:域名\用户名
PassWord
------解决方案--------------------
你可以换用 "User "试试。应该可以访问共享文件。

------解决方案--------------------
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Reflection;

namespace ImpersonateTest
{
class Class1
{
[DllImport( "advapi32.dll ")]
public static extern int LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

[DllImport( "kernel32.dll ")]
public extern static bool CloseHandle(IntPtr hToken);

static void Main(s)
{
Class1 c = new Class1();
if(c.Impersonate( "someuserOnRemoteSrv ", "RemoteSrv ", "hisPwd "))
{
string[] dirs = Directory.GetFiles(@ "\\RemoteSrv\xxxx ", "* ");
foreach (string dir in dirs)
Console.WriteLine(dir);
c.impersonationContext.Undo();
}
else
Console.WriteLine( "Impersonation failed ");
}

public bool Impersonate(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
// request default security provider a logon token with
LOGON32_LOGON_NEW_CREDENTIALS,
// token returned is impersonation token, no need to duplicate
if(LogonUser(userName, domain, password, 9, 0, ref token) != 0)
{
tempWindowsIdentity = new WindowsIdentity(token);
impersonationContext = tempWindowsIdentity.Impersonate();
// close impersonation token, no longer needed
CloseHandle(token);
if (impersonationContext != null)
return true;
}
return false; // Failed to impersonate.
}

WindowsImpersonationContext impersonationContext;
}

}

别人的写的代码。我没测试过。