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
Related
The question is pretty straight forward, I am making a Windows Service Program and the enviroment.getfolderpath isnt working.
Here is the code I have
string savePath = AppDomain.CurrentDomain.BaseDirectory; // this works
string savePath2 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // this returns an empty string...but why?
The documentation says
The path to the specified system special folder, if that folder
physically exists on your computer; otherwise, an empty string ("").A
folder will not physically exist if the operating system did not
create it, the existing folder was deleted, or the folder is a virtual
directory, such as My Computer, which does not correspond to a
physical path.
When running the Service as a Local System it doesn't run with any specific user permissions. Hence the GetFolderPath is returning empty because it is not able to recognize the path Desktop for LocalSystem.
You can either use Environment.SpecialFolder.CommonDesktopDirectory which will give C:\Users\Public\Desktop or
run the service with a specific user (in my case it's sampleuser) which will give the output as C:\Users\sampleuser\Desktop
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.
I have written a server in C# for a JS client.
The Solution consists restApi BL and DAL.
The BL creates links for images stored on a virtual directory, on the server.
The JS and the server code, are stored in the same directory.
When I build the string of the link I use this line of code:
string keyImageName = Settings.Default.ServerUrl +
Settings.Default.KeyImagesFolder + relatedFinding.KeyImagePath;`
where KeyImageFolder is a virtual directory.
It works fine, but my problem is that the website has multiple Amazon instances, one for each geographical zone , so every time I deploy, I need to change the ip in the settings.it's annoying.
Is there a way to get the virtual directory's url, specifically for each machine?
if the JS is installed on the same machine as the server, does it really need a full path?
Many thanks
First, you'll need to get the physical path for the file or directory that you want to generate a url for. This can be done within a Page object using Request.ApplicationPath.
Next, this path can be converted to a url path using the Server.MapPath function. This will take into account if there are more than one websites tied to the same path in IIS.
var physicalPath = Path.Combine(Request.ApplicationPath, Settings.Default.KeyImagesFolder, relatedFinding.KeyImagePath);
var resourceUrl = Server.MapPath(physicalPath);
I have a code snippet that loads some assemblies from the current executing directory at runtime.
The code is part of a library that can be hosted in a console app/windows service/aspnet web app etc.
Is there a single API call that will provide the current directory the code is running from?
For a console/windows service AppDomain.CurrentDomain.BaseDirectory; returns the correct
path but the same call in an ASPNET app returns the path to the virtual root instead of the path
to the [virtualroot]\bin directory of the web app.
For ASPNET AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; returns what I want.
I could make a check like the following so that I get the correct path irrespective of the host:
string path = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath ??
AppDomain.CurrentDomain.BaseDirectory;
This sort of heuristic seems a bit hackish and I was hoping there is a single API call that will provide the expected results
To check where the current assebly is stored you can use this:
Assembly.GetExecutingAssembly().CodeBase
Or if your code is in a library and you wand the location of the started exe you can use GetEntryAssembly().
The codeBase is in URI syntax. To get the path (if required) you can use:
var path = new Uri(codeBase).AbsolutePath;
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.