Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Network storage is on 192.168.1.1 server
and windows service is installed on 192.168.1.2 server

Please help...
Posted
Comments
Richard MacCutchan 20-Feb-15 3:54am    
Make the drives visible to the other system.

here is the code snippet for the same:
C#
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;
        }
 
Share this answer
 
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[^]
 
Share this answer
 
Comments
Member 11465756 20-Feb-15 6:21am    
i had used UNC path then also i am not able to access network drive folders

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900