I am using windows application to send mail with the attachment. But at this line of code I get error as
The name 'server' does not exist in current context
var directory = Server.MapPath("Attachment/");
I tried from here but it was not working for me
You are trying to use a Server object in a windows application, the Server object is for web applications only.
You can simply specify the full local path to the directory and use this, unlike having to map it based on a virtual directory as in a web application.
If the path to your files/directory are contained in the same folder as your application, you can use Application.StartupPath property and combine this with your own values to get a full path.
var directory = Application.StartupPath + "\\Attachment\\";
Related
I have a UWP project, and wrote this code:
foreach (var foldertype in (Environment.SpecialFolder[])Enum.GetValues(typeof(Environment.SpecialFolder)))
{
//string d = Environment.CurrentDirectory;
var path = Environment.GetFolderPath(foldertype);
var folder = await StorageFolder.GetFolderFromPathAsync(path);
StorageApplicationPermissions.FutureAccessList.Add(folder, folder.Path);
Debug.WriteLine($"Opened the folder: {folder.DisplayName}");
this.MenuFolderItems.Add(new MenuFolderItem(folder));
}
It is supposed to enumerate all the special folders, and get their folder. However, while debugging, this is what happens:
foldertype = Desktop
path = "C:\\Users\\cuent\\AppData\\Local\\Packages\\402b6149-1adf-4994-abc9-504111b3b972_a5s740xv383r0\\LocalState\\Desktop"
folder = [ERROR] System.IO.FileNotFoundException: 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'
I do not know what is happening here, it seems to be appending the path to the installed location of the app. How do I fix this?
Expected output of GetFolderPath() is wherever the Desktop is, not the weird path.
UWP apps are different from desktop applications when accessing the file system. UWP apps are running in the sandbox so there are limitations for UWP apps when trying to access the file system. You could check this document: File access permissions. The document lists all the locations that UWP apps have permission to access.
Back to your scenario, what you need first is a broadFileSystemAccess restricted capability. This capability enables your app could use the StorageFolder.GetFolderFromPathAsync() API with a Path parameter. This is mentioned in the last part of the document I posted above.
Then the second issue is the Environment.GetFolderPath API. It looks like the API will return a Path that points to a local folder inside the app's local folder. But there is no such desktop folder inside the app's local folder so you will get the FileNotFoundException. You might need to set the correct path by yourself like C:\Users\your user name\Desktop. After that, your code should be able to work correctly.
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
I'm working on a c# web application which is hosted on an server 192.168.X.Y. In this application I'm accessing few documents which are inside a folder on a server 192.168.X.YYY\Folder(I'm providing this info in the Web.Config file). But the actual path of 192.168.X.YYY\Folder has a different name like abc.Application.com\Application1\Folder. How can I get this name in my c# application? Because I want to open the documents in this folder using google document viewer.
TIA :)
I used Request.ServerVariables["HTTP_HOST"] which served my purpose.
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".")1 returns the current physical directory of the file is being executed
Server.MapPath("..") returns the parent directory
Server.MapPath("~") returns the physical path to the root of the application
Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
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;