Directory.SetCurrentDirectory() messed up when remote debug - c#

When doing remote debugging. I share the directory say "d:\debug\app" on my remote debug machine to host machine.
On my host machine I setup the debug setting to use remote machine: "user#machine"
and start external program "\\machine\debug\app\start.exe"
When following program runs:
string codeBase = Assembly.GetExecutingAssembly().CodeBase;//codeBase="file://machine/debug/app/lib.dll"
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
Directory.SetCurrentDirectory(Path.GetDirectoryName(path));//Path.GetDirectoryName(path)="\\debug\\app"
getting error:
System.IO.DirectoryNotFoundException was unhandled
Message="Could not find a part of the path '\\\\machine\\debug\\debug\\app'."
Looks like the Directory.SetCurrentDirectory() is taking the shared folder as root. and trying to do "\machine\debug" + "debug\app"
How to fix this?
It's not a problem of formate.
There is a similar question here: How do I convert a UNC path back to an absolute local path on the remote PC?
But, the author give a self code solution. I would like something easy with existing API and tools.

Related

Server.Mappath() fail on remote server

i use like this codes for prepare paths.
string path = Server.MapPath("~/"+ User.Identity.Name+ "/file.zip");
i take this error when i run this code on localhost
Index:582 Not allowed to load local resource: file:///C:/Users/trkmml/Documents/GitHub/OfisTakip/trkmml/file.zip
i think its not problem (its chrome security thing). anyway file path is correct.
On the other hand, when I upload the program to hosting, it adds "file:///C:/Inetpub/vhosts" to the front of the link. how can i cancel this part. how do I make it just
http://example.com/httpdocs/trkmml/file.zip".
here is error on remote host
Not allowed to load local resource: file:///C:/Inetpub/vhosts/example.com/httpdocs/trkmml/file.zip

Can't see path to another server in ASP.NET MVC

I have an internal ASP.NET MVC site that needs to read an Excel file. The file is on a different server from the one that ASP.NET MVC is running on and in order to prevent access problems I'm trying to copy it to the ASP.NET MVC server.
It works OK on my dev machine but when it is deployed to the server it can't see the path.
This is the chopped down code from the model (C#):
string fPath = HttpContext.Current.Server.MapPath(#"/virtualdir");
string fName = fPath + "test.xlsm";
if (System.IO.File.Exists(fName))
{
// Copy the file and do what's necessary
}
else
{
if (!Directory.Exists(fPath))
throw new Exception($"Directory not found: {fPath} ");
else
throw new Exception($"File not found: {fName } ");
}
The error I'm getting is
Directory not found:
followed by the path.
The path in the error is correct - I've copied and pasted it into explorer and it resolves OK.
I've tried using the full UNC path, a mapped network drive and a virtual directory (as in the code above). Where required these were given network admin rights (to test only!) but still nothing has worked.
The internal website is using pass through authentication but I've used specific credentials with full admin rights for the virtual directory, and the virtual dir in IIS expands OK to the required folder.
I've also tried giving the application pool (which runs in Integrated mode) full network admin rights.
I'm kind of hoping I've just overlooked something simple and this isn't a 'security feature'.
I found this question copy files between servers asp.net mvc but the answer was to use FTP and I don't want to go down that route if I can avoid it.
Any assistance will be much appreciated.
First, To be on the safe side that your directory is building correctly, I would use the Path.Combine.
string fName = Path.Combine(fPath, "test.xlsm")
Second, I would check the following post and try some things there as it seems to be a similar issue.
Directory.Exists not working for a network path
If you are still not able to see the directory, there is a good chance the user does not have access to that network path. Likely what happened is the app pool running your application has access to the directory on the server. The production box likely doesn't have that same access. You would have to get with the network engineer to get that resolved.
Alternatively, you could write a Powershell script to run as a user who has access to both the production and the development server to copy the file over to the production server if that is your ultimate goal and your server administrators could schedule it for you if that is allowed in your environment.

c# absolute path shared drive

I am trying to get the path of my WCF service folder hosted on my web server using C# code. I am using below logic to get the path:
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path1 = Uri.UnescapeDataString(uri.Path);
string path2 = Path.GetDirectoryName(path1);
path2 = path2.Substring(0, path2.LastIndexOf("\\"));
When I run this code on my local machine, it gives me correct path starting from the drive letter like "D:\appdir\servicehost\". However, when I run it on my web server, it does not work as expected because the IIS virtual directory on my web server is pointing to a shared location pointing to some other machine. In this case, the initial IP address part is omitted and it directly starts from the shared drive name, like
"\SharedFolder\servicehost\"
Instead, I am expecting the code to return the whole path. When the service is hosted on a local drive, it should give me "D:....." and when it is hosted on a shared drive then it should give me the path including the IP address like "\\10.44.22.11\SharedFolder\servicehost"
This is causing a file load logic to fail on my web server having the same code as my local machine because it does not find the file located at a wrong location which excludes the IP address.
Hence, I decided to use the "URI" string which contains the whole path of the dll file starting from "file://..". So I can cut the "file:" part and the dll name part of the string and get the whole path. But this does not seem to be the right way and I am sure there will be the more sophisticated way to get it worked in both cases.
Is there any common way of coding this, which I can use in both these scenarios to get the full path?
I created a new IIS App Pool as per the image:
Important thing for me was to set the NetworkService as the identity account otherwise I wasn't getting access to the network.
Then I created a share on another server to hold my TestWcfService and pointed the IIS Application at it:
In addition, the ACLs on the file system are set to EVERYONE, FULL CONTROL for ease of testing.
The service has this line:
String result = String.Format("HostingEnvironment.MapPath: {0}", HostingEnvironment.MapPath("~\\MyFile.txt"));
Resulting in this output in my testing client:
HostingEnvironment.MapPath: \\Company\Shares\1 - JayV\TestWcfService\MyFile.txt
To access the file internally within the Wcf Service, you use the return value of HostingEnvironment.MapPath as above.
To access the file remotely, from a client talking to your web service use: http://localhost/Remote/myfile.txt
My Virtual Application has no other virtual sub-directories, the Wcf Service is run from this Virtual Application and not from the root web site

Getting the System.IO.DirectoryNotFoundException when run the app through cron job

I have written one app which read the one file from network drive and parse the data. This application work fine when I manually running it. when I running this app through window 7 scheduler then its giving the System.IO.DirectoryNotFoundException.
I have hard coded the network drive path like "U:\\scripts\\file.txt". I have also change the file permissions to 777 but still getting the same error. code is in C#.
Exception occur in below line.
fileName = "U:\\scripts\\file.txt";
var lines = File.ReadAllLines(fileName); //exception in this line
When you map a drive in Windows, it is only mapped for the current user. Scheduled tasks don't have access to mapped drives. The simplest solution is to reference the remote file using the full UNC path. For example, if U: is mapped to \\server\directory\ then your full UNC path would be:
\\server\directory\scripts\file.txt

Upload files to a remote server

I need to upload files from my asp.net (C#) page residing in the web server to a remote server.
I managed to upload files to remote server from localhost using this code:
string serverPath = "\\\\xx.xxx.xx.xx\\Folder\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(serverPath);
But after I published this code to my web server, it stopped working with the error "The network path was not found."
I have looked at a few solutions which suggest using UNC network share and implementing impersonation.
I couldn't figure out how to apply these solutions.
Can someone please give an example, or suggest a simpler solution.
Thanks!!
In FileUpload1.PostedFile.SaveAs(path), path is physical path of file, No Url. You must check:
is Physical folder Exsist?
is You have access to folder?
if answer of both question is true check this code:
string serverPath = #"\\xxx.xxx.xxx.xxx\Folder\";
if (!System.IO.Directory.Exists(serverPath))
System.IO.Directory.CreateDirectory(serverPath);
FileUpload1.PostedFile.SaveAs(serverPath + FileUpload1.FileName);
The account your application runs under must have write permissions to the folder you are trying to upload the file to: \\xx.xxx.xx.xx\Folder\. So you will have to configure the application pool in IIS to run under an account that will have sufficient permissions. Go to the application pool properties in the IIS management console where you will be able to specify an account to be used to run the application. By default it uses a built-in account which won't have any access to shared resources. Take a look at the following article which explains how to do so.
You need a virtual directory on your webserver to upload to. In code you'll have to use Server.Mappath("virtual path") function to get its server path and then save to it.

Categories