如何通过安装在不同服务器上的窗口服务访问网络驱动器文件夹 [英] how to access a network drive folders through window service which is installed on different server
本文介绍了如何通过安装在不同服务器上的窗口服务访问网络驱动器文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
网络存储在192.168.1.1服务器上
和Windows服务安装在192.168.1.2服务器上
请帮助...
Network storage is on 192.168.1.1 server
and windows service is installed on 192.168.1.2 server
Please help...
推荐答案
这里是相同的代码片段:
here is the code snippet for the same:
public static void SaveACopyfileToServer(string filePath, string savePath)
{
var directory = Path.GetDirectoryName(savePath).Trim();
var username = "loginusername";
var password = "loginpassword";
var filenameToSave = Path.GetFileName(savePath);
if (!directory.EndsWith("\\"))
filenameToSave = "\\" + filenameToSave;
var command = "NET USE " + directory + " /delete";
ExecuteCommand(command, 5000);
command = "NET USE " + directory + " /user:" + username + " " + password;
ExecuteCommand(command, 5000);
command = " copy \"" + filePath + "\" \"" + directory + filenameToSave + "\"";
ExecuteCommand(command, 5000);
command = "NET USE " + directory + " /delete";
ExecuteCommand(command, 5000);
}
public static int ExecuteCommand(string command, int timeout)
{
var processInfo = new ProcessStartInfo("cmd.exe", "/C " + command)
{
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = "C:\\",
};
var process = Process.Start(processInfo);
process.WaitForExit(timeout);
var exitCode = process.ExitCode;
process.Close();
return exitCode;
}
您好
您不能直接使用网络映射路径从Windows服务。你必须使用UNC路径。参考下面的文章和讨论相同的
http://blog.stephencleary.com/2009/10/windows-services-and-network.html [ ^ ]
UNC [ ^ ]
https://social.msdn.microsoft.com/Forums/vstudio/en-US/ 6c742c34-2abc-42f4-ab41-371cfb7057cf / windows-service-cant-access-network-locations [ ^ ]
Hi
you are not suppose to use network mapped paths directly from an windows service. you must use UNC paths. refer below articles and discussions on the same
http://blog.stephencleary.com/2009/10/windows-services-and-network.html[^]
UNC[^]
https://social.msdn.microsoft.com/Forums/vstudio/en-US/6c742c34-2abc-42f4-ab41-371cfb7057cf/windows-service-cant-access-network-locations[^]
这篇关于如何通过安装在不同服务器上的窗口服务访问网络驱动器文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文