This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
HttpContext.Current.Server null
I have three projects in my ASP .NET solution:
a class library
a web application
a windows service
I have a folder in my web application with an XML file. An XMLreader from class library function needs to be populated with this XML file. I have added this:
var reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings);
When this function is called from we application, It works.
When this function of class library is called from windows application, I get null reference at HttpContext.Current.Server because it is not available in windows application. I don't want to give hardcode path although that works for both windows and web application. Can I use some don't function to access files from different projects or any alternate of server.mappath. Please suggest solution
Thanks
System.AppDomain.CurrentDomain.BaseDirectory should do the trick.
http://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory.aspx
Gets the base directory that the
assembly resolver uses to probe for
assemblies.
HttpContext.Current.Server.MapPath alternative in windows application
file path using C#
HttpContext.Current.Server null
Asking the same basic question four different times isn't going to change the fact that you cannot use a Windows Service to access the web application path and the web app isn't going to know where your service is.
Use a configuration file to define where your data will be stored and provide the same info to both - or use some other storage for your data like a database which has a published location.
The simplest solution is probably to pass the path to the xml file into that function, rather than making the function itself clever enough to figure it out- then your web app could use the HttpContext method, and the Winforms app would use a regular path. There is no equivalent of the MapPath method for WinForms or windows services.
Related
Hello fellow programmers.
I recently made a mistake while using the SQLite-NET package for Windows Phone 8.1. When opening a new SQLiteConnection, I would give as parameter to its constructor the string "data.db", without being actually aware of what was going on under the hood. Later on, I noticed that the file wouldn't be deleted, even if I uninstalled the application (since I would have in my app the same entries that I had before uninstalling it); I assume that I should be using the local folder for application data instead.
However, here is the real question: what the hell is the folder where the data.db file was created? I tried to figure it out with the following piece of code:
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///data.db"));
System.Diagnostics.Debug.WriteLine(file.Path);
and what I got as output of the WriteLine method was the path
C:\Data\SharedData\PhoneTools\AppxLayouts\f7529f24-ba24-4fdb-8353-cff9214180a0VS.Debug_ARM.Valbrand\data.db,
which only got me even more confused. I couldn't find any satisfactory info on this, and I would REALLY like to further my understanding of what happened here.
Thanks in advance!
The path you are looking at is the install directory of your app. You have a Silverlight 8.1 app, so the path is slightly different than a Silverlight 8.0 app, but the articles here and here will help you get a rough understanding.
The recommended usage of this directory is to take the resources that you want backed up out of it on first run and move them to the app data container, so they can be backed up by the system.
C:\Data\SharedData\PhoneTools\AppxLayouts\f7529f24-ba24-4fdb-8353-cff9214180a0VS.Debug_ARM.Valbrand\data.db
This is the path on your phone/emulator.
And This is not the location where you can find in your PC.
I spent some reading to figure out how to perform a complete file upload process with Windows RT. The "circumstances" regarding StorageFolder, StorageFile, security box model and such stuff is (at least known, but well known) subjects. Perhaps I started at wrong end, but I hope experts out there can help this question a meaning.
I have a
- Windows 8 App Store project.
- WinRT Business DLL Project.
- WinRT Unit Test Project.
This will keep me sure that i work with WinRT related objects.
The question is:
How do I perform a file stream from my test project to logic in Business DLL project? So I can i.e. stream the uploaded file to application temp folder?
As a paranthese. From within the Windows 8 App Store Project I'm honestly also try to success let the FileOpenPicker give me the stream, not just the filename. The question attached below added some useful information. Though i feel it's more verbose then needed?!
save stream to file in c# and winrt
From you test project, you can use FileIO.ReadBufferAsync to get data into an IBuffer. Once you have that, you can read or write it with a DataReader or DataWriter as you please. Check out this example on how to manipulate the data with readers and writers.
Instead of me writing from scratch a text file sharing windows form application, I thought I would simply ask if someone has a snippet (C#) or methodology that I could use?
The application merely needs to create a text file and allow multiple users using the same application from different pcs on the network to append comments to the file. Basically, I am trying to get multiple users to give frequent updates/comments/status changes to a project with this simple app.
I have looked into some filestream locking suggestions, but again, would like to see if anyone would be willing to share a code snippet.
Thanks in advance for any assistance.
Dell
Try using System.IO.File.AppendText method this does everything you are looking for file operation
Remarks taken from MSDN
StreamWriter(String, Boolean) constructor overload. If the file
specified by path does not exist, it is created. If the file does
exist, write operations to the StreamWriter append text to the file.
Additional threads are permitted to read the file while it is open.
The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to
the current working directory. To obtain the current working
directory, see GetCurrentDirectory.
The path parameter is not case-sensitive.This method is equivalent to the
Use Client-Server approach, for example make a WCF service with 2 methods AppendText and GetText, and then only WCF service would write to file.
Here is the example of basic WCF client-server
http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication
I have finished developing an executable desktop application to generate a fractal image based on the passed-in arguments. The output type is JPEG.
Now I am developing a site under ASP.NET MVC 3. I want to use the executable from within my site.
Is it possible to use it as is without converting it to a class library and recompiling?
If you mean "to run on the user's machine", then not "as is" - you might want to look at Silverlight for that.
If you mean "to use at the server", then it will of course depend on how it operates (whether it prompts for input etc), and how it works (does it use GDI+, for example? that isn't recommended for use on web servers).
But sure; you can shell an exe with Process.Start, or if it is a .NET exe you can either add a reference directly to the exe and use it as a library (if it has appropriate code)
There is also a way to run it in-process via AppDomain.ExecuteAssembly - not sure this latter is a good idea on a web-server, though... especially if the exe talks to stdout.
For getting the image back to the client, you would want this processing to happen (perhaps with caching) in a route that uses return File(...); from MVC to simulate the image stream.
Use the system.Diagonstics.Process class to call the exe with a StartInfo object to pass the appropriate arguments.
Is there a standard way in .Net to access and modify the windows services file? Right now I'm just accessing the file via a stream and writing to the file like I would for any other file. The file is located in c:\windows\system32\drivers\etc\, but I'm worried that this may change in 64 bit versions of Windows or may vary in different versions of Windows (I could be wrong and admittedly, I haven't looked into this very much yet). Aside from that, I'm just wondering if there is a standard way, say via WMI and/or the System.Management namespace, to find and modify the services file.
The actual specifics of what I need to do is to check if certain database aliases used for our software are specified for the expected ports. If not, add them.
An open source project called System.Peppers has a class doing this.
There is a registry key that contains the full path to the files you are editing.
Here is a link to the exact class: HostFile class
Use the System.Management.Instrumentation namespace
Sample code here
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=114
you can use System.Environment.SystemDirectory to get to the sys32 folder