How create Path on a Live server in C# code? - c#

The below code shows how I created a Path/Directory to Local Machine. Now, I want to put my Application to the Live server but my problem it creates a File to a given Path on a Live server, how can I achieve it?
`string appPath = Request.PhysicalApplicationPath;
string IPAddress = HttpContext.Current.Request.UserHostAddress;
Directory.CreateDirectory(appPath + "//PrintFiles/" + IPAddress");
StreamWriter w;
w = File.CreateText(appPath + "//PrintLabels/" + IPAddress + "/printLabels.txt");
w.WriteLine(fileContents.ToString());
w.Flush();
w.Close();`

Same code will work if the server has access to the target location. But the process running the code has to have rights which will allow for directory creation at the target location. If it is a local process to a non local resource, then the process needs either needs to be known to both computers or a system wide process should kick off the code.
For example if this process is in IIS, then the app pool user needs to be set to a process which has access rights to the target location.

Related

Copy entire content of a directory from local to remote machines in C#

I tried to create a C# Windows Forms tool to copy entire content from my local machine to another machines on the domain.
The problem that most solutions to copy just file not entire directory is not working.
I can't using Impersonation to change user context and then use File.Copy() to copy content cause the users on the remote machines different than the user on my local machine.
using ( new Impersonator( "yourUsername", "yourDomain", "yourPassword" ) )
{
// The following code is executed under the impersonated user.
}
Also to use File.Copy() like that:
File.Copy(
#"C:\Users\user\Desktop\file.txt",
#"\\remote\Users\user\Desktop\file.txt"
);
the remote path should be a shared path, and this is not the case here as i have a credentials to servers but want to copy the directory to specific not shared location.
I found another solution that uses PSexec tool to activate listener on the server and then send files to it from my local machine (Socket programming) but it's not stable: you can find it here
I also think about writing a powershell script that can do that and i run it from my C# program.
So is it another solution for doing that in a correct way, Copying entire directory/directories from local to remote machines.

Batch file not working in server but working local

I created a batch file in my asp.net web site, It is working within the local system without any problem. After I was upload it to my web site's server (shared server), it is not working in the remote environment.
Here is a screenshot of the error I'm receiving
This is the code I'm using to test run the batch on the server.
string path = Server.MapPath("~/SourceCode/Jsil");
Response.Write(path+"<br>");
string batpath=Server.MapPath("~/Dir.bat");
string framework = Server.MapPath("~/SourceCode/v4.0.30319");
string vscompiler = #"\csc.exe /t:exe /r:JSIL.dll;JSIL.Meta.dll Program.cs";
string full = framework + vscompiler;
Response.Write(full);
StreamWriter file = new StreamWriter(batpath);
file.WriteLine("G:");
file.WriteLine("cd " + path);
file.Write(full);
file.Close();
//Excecute bat file
System.Diagnostics.Process compile = new System.Diagnostics.Process();
compile.StartInfo = new ProcessStartInfo(batpath);
compile.Start();
compile.Close();
This might be a simple problem of privileges. The application user might not have execute privilege in the server box. You need to contact your system administrator and ask him to provide execute privilege to the IIS app pool user in the server.

How to copy files from client system directory to server system directory via remoting

I am copying files from one path to another path in my pc as
string destpath= Application.StartupPath + #"\Multiple Documents1";
string soucepath= #"D:\Naresh WORK AREA\Naresh\Multiple Documents1";
if (!File.Exists(path))
{
DirectoryInfo dir = Directory.CreateDirectory(path);
}
System.IO.File.Copy(soucepath, destpath+#"\"+filename.doc, true);
It works fine in single user inveronment. Now I am using remoting with windows service, I want to save the file into server i'e where my service is running. If I take my source path to the server, it will not identify the sourcepath as the client system path. I know the destination path is service startuppath. But How can I take my source path to service running system.
Instead of using local absolute paths you can use UNC paths as in \ComputerName\ShareName. The account your Windows Service is running under of course Needs proper permissions to access the Shares.
When configuring your service, use the perspective of the server the service is running on. So you need to use a UNC path for the client directory, but you can keep the absolute path on the server side.
You can set up a new share on the client computer in Windows Explorer.

Access problems to a local folder via a network share

I have a Windows service, running using the login localhost\administrator. This service is meant to invoke another executable (MyProcess.exe), which is supposed to write some stuff to a log file. The service uses Process.Start() to create the process, like so:
var p = new Process();
p.StartInfo.FileName = processFileName;
p.StartInfo.Arguments = arg;
p.Start();
Problem is, it appears that MyProcess.exe is being denied rights to write to the log file, even though localhost\administrator unquestionably has rights to the log folder. If I run MyProcess.exe from a command line, it works perfectly.
So, is it possible that the process is being executed using a different user login?
Can you think of any other reason why MyProcess.exe is being denied rights to write the log file?
UPDATE: the log file is being written to the local machine, but using a network address, i.e. \\MyPC\LogFolder. When I change the code to refer to C:\MyFolder, everything works fine. It's obviously having a problem with the network address (even though it's local).
What sharing settings do I need to put on the folder so that the local system account can access the file?
If you are using impersonating, than it impersonates a user that can be the currrent or a specified user. if not it will run under the Local System, with the privileges of the local system.
p.StartInfo.Domain = "UserName";
p.StartInfo.Password = "Passw0rd!";
You can get the username from:
Thread.CurrentPrincipal.Identity.Name
I've worked it out.
The problem, as noted in my update, is that the process was addressing the log folder using a network share address, \\MyPC\LogFolder, and when we switched the configuration so that it wrote instead to c:\Logfolder, it worked fine.
So it seems that when you address a local folder, the localhost\Administrator account is deemed to have sufficient rights. But when you go via the network share, you need to present valid network credentials, and localhost\Administrator just doesn't cut it. If you change to use MYDOMAIN\MyUser, it works even using the network share address.

Renaming a file on a remote file server in C# / Python

I need to rename a whole heap of files on a Windows file server - I don't mind what language I use really as long it's quick and easy!
I know it's basic but just to clarify - in pseudo-code...
server = login (fileserver, creds)
foreach (file in server.navigateToDir(dir))
rename(file)
I know how to do this in Python/C# if I was a local user but have no idea if it's even possible to do this remotely using Python. I've searched for code snippets/help but have found none yet.
Thanks.
Use \\servername\sharename\somefile.foo for filenames - provided you have access to connect to it and are running on windows.
You could also map up a network drive and treat it as any other local drive (y:\sharename\somefile.foo)
You could also use PSEXEC to execute the code remotely on the server if you need the performance of locally executed code. See http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
Have a look at pyfilesytem, it provides a consistent interface for local and remote filesystems.
The following renames a file in each of the sub-directories of the folder path given. It renames the file from the given filename (eg."blah.txt") to foldername+extension.
NB. Z can be either a local or network drive (ie. if folder is on file server map network drive to it).
For example from a shell...
python renamer.py "Z:\\FolderCollectionInHere" blah.txt csv
... will rename a file 'blah.txt' in each immediate sub-directory of "Z:\FolderCollectionHere" to .csv.
import os
import sys
class Renamer:
def start(self, args):
os.chdir(args[1])
dirs = os.listdir(".")
for dir in dirs:
try:
os.rename(dir + "\\" + args[2], dir + "\\" + dir + "." + args[3])
print "Renamed file in directory: " + dir
except Exception:
print "Couldn't find file to rename in directory: " + dir
Renamer().start(sys.argv)

Categories

Resources