我想请教一下,C#中如何将本机的数据复制到远程式的服务器中? 已经用 Management 连接上服务器了.
需求是这样的,公司的服务器上有共享一些文件夹. 那普通用户对这些文件夹是只读的, 我需要在我的程序里让普通用户对服务器上这些共享文件夹有读写和修改的权限,管理员权限的帐号是写死在我的程序里的.
代码如下: (远程计算机连接是正常的,查询什么的,操作都是好的, 就是在 File.Copy 时总是报没权限)
if (Clipboard.ContainsFileDropList())
{
System.Collections.Specialized.StringCollection sc = Clipboard.GetFileDropList();
for (int i = 0; i < sc.Count; i++)
{
if (File.Exists(sc[i]))
{
FileInfo fi = new FileInfo(sc[i]);
ConnectionOptions options = new ConnectionOptions();
options.Username = "Administrator"; //给定管理员帐号
options.Password = "123456"; //给定管理员帐号的密码
ManagementScope scope = new ManagementScope(@"\\192.149.200.18\root\cimv2", options);
scope.Connect(); //用给定的管理员用户名和密码连接远程的计算机
string sPath = @"\\192.149.200.18\root\cimv2:Win32_LogicalDisk='D:'";
ManagementPath mp = new ManagementPath(sPath);
ManagementObject disk = new ManagementObject(scope, mp, null);
disk.Get();
File.Copy(sc[i], @"\\192.149.200.18\D$\Label\" + fi.Name); //这边总是报没权限
}
}
}
需求是这样的,公司的服务器上有共享一些文件夹. 那普通用户对这些文件夹是只读的, 我需要在我的程序里让普通用户对服务器上这些共享文件夹有读写和修改的权限,管理员权限的帐号是写死在我的程序里的.
代码如下: (远程计算机连接是正常的,查询什么的,操作都是好的, 就是在 File.Copy 时总是报没权限)
if (Clipboard.ContainsFileDropList())
{
System.Collections.Specialized.StringCollection sc = Clipboard.GetFileDropList();
for (int i = 0; i < sc.Count; i++)
{
if (File.Exists(sc[i]))
{
FileInfo fi = new FileInfo(sc[i]);
ConnectionOptions options = new ConnectionOptions();
options.Username = "Administrator"; //给定管理员帐号
options.Password = "123456"; //给定管理员帐号的密码
ManagementScope scope = new ManagementScope(@"\\192.149.200.18\root\cimv2", options);
scope.Connect(); //用给定的管理员用户名和密码连接远程的计算机
string sPath = @"\\192.149.200.18\root\cimv2:Win32_LogicalDisk='D:'";
ManagementPath mp = new ManagementPath(sPath);
ManagementObject disk = new ManagementObject(scope, mp, null);
disk.Get();
File.Copy(sc[i], @"\\192.149.200.18\D$\Label\" + fi.Name); //这边总是报没权限
}
}
}
现在的问题就是 如何将本地的文件或文件夹能够复制到服务器上.
不管用什么方法来实现.
不过,有一点,因为使用程序的可能一些人只是普通用户权限,对这些共享文件夹只有读的权限,只能用我的程序来对服务器文件进行写或修改. 我不想在我的程序执行后,留下什么漏洞什么的,如映射网络磁盘,如果没有被关掉的话,那别人就可以对我服务器里的一些文件夹任意操作了. 我只是想通过我的程序进行管控,并针对性的给个别文件夹的写或修改权限,在我的程序里.
不管用什么方法, 请各位给点建议撒...
/// <summary>
/// 共享文件夹信息
/// 0:共享文件夹名
/// 1:共享路径
/// 2:共享描述
/// 3:共享状态
/// 4:文件夹位于共享计算机的实际路径
/// </summary>
private string[,] shareFiles = null;
/// <summary>
/// 存储共享计算机中的实际路径
/// </summary>
private string strPath = string.Empty;
/// <summary>
/// 存储当前共享路径
/// </summary>
private string sharePath = string.Empty; /// <summary>
/// 登陆远程计算机
/// </summary>
/// <param name="sComputer">远程计算机的计算机名或IP地址</param>
/// <param name="sUsername">计算机用户帐号</param>
/// <param name="sPassword">计算机用户密码</param>
/// <returns></returns>
private ManagementScope LoginComputer(string sComputer, string sUsername, string sPassword)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = sUsername; //给定管理员帐号
options.Password = sPassword; //给定管理员帐号的密码
ManagementScope scope = new ManagementScope(sComputer + "\\root\\CIMV2", options);
scope.Connect(); //用给定的管理员用户名和密码连接远程的计算机
return scope;
} /// <summary>
/// 登陆远程计算机并获取该计算机中所有共享文件的信息
/// </summary>
/// <param name="sPC">远程计算机的计算机名或IP地址</param>
/// <param name="sUid">计算机用户帐号</param>
/// <param name="sPwd">计算机用户密码</param>
/// <returns></returns>
private string[,] GetShareFiles(string sPC, string sUid, string sPwd)
{
ManagementScope scope = LoginComputer(sPC, sUid, sPwd);
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Share"); // 查询计算机中所有共享文件夹
ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, query);
foreach (ManagementObject mobj in mos.Get())
{
string sPath = mobj["Path"].ToString(); // 共享文件夹路径
string sName = mobj["Name"].ToString(); // 共享文件夹名
string sCaption = mobj["Caption"].ToString();
string sDescription = mobj["Description"].ToString();
string sStatus = mobj["Status"].ToString();
if (sPath != string.Empty && sName.IndexOf("$") < 0)
{
if (this.IsPath(sPath))
{
sPath = sPC + @"\" + sName;
listFolders.Add(new string[5] { sName, sPath, mobj["Description"].ToString(), mobj["Status"].ToString(),mobj["Path"].ToString()});
}
}
}
listFolders.TrimToSize();
shareFiles = new string[listFolders.Count, 5];
for (int i = 0; i < listFolders.Count; i++)
{
shareFiles[i, 0] = ((string[])listFolders[i])[0];
shareFiles[i, 1] = ((string[])listFolders[i])[1];
shareFiles[i, 2] = ((string[])listFolders[i])[2];
shareFiles[i, 3] = ((string[])listFolders[i])[3];
shareFiles[i, 4] = ((string[])listFolders[i])[4];
}
return shareFiles;
} /// <summary>
/// 检查字符串是否为路径格式
/// </summary>
/// <param name="str">要检查的字符串</param>
/// <returns>True:是路径格式的字符串</returns>
private bool IsPath(string str)
{
bool rt = false; if (str.IndexOf(@"\") > -1 || str.IndexOf(":") > -1)
{
rt = true;
} return rt;
}
http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008324/106313_2.html