Skip to content

Instantly share code, notes, and snippets.

@jinan-kordab
Last active March 24, 2024 18:46
Show Gist options
  • Save jinan-kordab/01a6412a1f52b4ca5b505299304d96b2 to your computer and use it in GitHub Desktop.
Save jinan-kordab/01a6412a1f52b4ca5b505299304d96b2 to your computer and use it in GitHub Desktop.
CopyFile C# code that uses Network Share class to copy any file to your remote SHARED folder. :)
//this is a comment: // Supposedly your server where you want to copy your file is: 10.10.10.10
string unme = "administrator";
string password = "Y0Ou)rS0Ec)R0Ec)Y";
NetworkShare.ConnectToShare(@"\\10.10.10.10\YourSharedFolder", unme, password); //Connect with the new credentials
//check for the file existance first
if (System.IO.File.Exists(@"\\10.10.10.10\YourSharedFolder\Subfolder" + fileName))
{
//set attributes to normal because by default files are read only, but in our case we need to delete it first
System.IO.File.SetAttributes(@"\\10.10.10.10\YourSharedFolder\Subfolder" + fileName, FileAttributes.Normal);
//if it exists delete it
System.IO.File.Delete(@"\\10.10.10.10\YourSharedFolder\Subfolder" + fileName);
//copy new file
System.IO.File.Copy(currentEmgMessagePath, @"\\10.10.10.10\YourSharedFolder\Subfolder" + fileName + "");
//close network share
NetworkShare.DisconnectFromShare(@"\\10.10.10.10\YourSharedFolder", true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment