Can access private file in localhost, but not in production - c#

I'm writing an ASP.NET Core app, and I'm trying to use a file's path without exposing it to the web. I set it to copy to output directory, so it loads when I test it on localhost, but it gives me an error when published to the web:
The system cannot find the file specified.
This is the code that generates the path:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", "filename.p12");
I've also tried
Path.Combine(Environment.CurrentDirectory, "Config", "filename.p12");
I'm guessing AppDomain.CurrentDomain.BaseDirectory and Environment.CurrentDirectory point to a different location when published? Or filename.p12's 'Copy to Output Directory' property doesn't carry over to the published version?
EDIT:
I also tried:
Path.Combine(environment.ContentRootPath, "Config", "file.p12");
Where environment is an IHostEnvironment, to the same effect. I'm getting the feeling that the file isn't being copied over into the published version. So, I changed the filename I put in this question to be file.p12 instead of file.txt, which is what I originally put it as, in case it doesn't copy over that type of file for security reasons.

The error said The system cannot find the file specified., but the file was actually there. I was actually missing the X509KeyStorageFlags.MachineKeySet flag when I read the .p12 file. I think the reason it didn't work in production was that the server's current user didn't own the file, so I needed to specify that the machine owns it with that flag.

Related

C# File Disappeared after FileInfo.MoveTo with Local Path (Windows)

I Ran FileInfo.MoveTo("filename.txt") with just a name instead of passing a full path and the file just disappeared. I believe in linux this would make it go to the root directory "/", but on Windows I'm not sure if there is a such thing as a root directory beyond just C: Is there any way to locate my lost file?
It is likely in the working folder that your executable is running from. For example, MyApp\bin\Debug, depending on the configuration you are running in.
It should be in project folder. Usually files without specefying path are saved there. (in folder with .exe file)

Finding a specific file path when two files have the same name in different locations in C#

I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.

File or directory not found although it exists and the path is correct

I have a problem - I cannot find my file when I open it using http://localhost:49652/.well-known/acme-challenge/FfI7Xeq7_QH5R5pCd3LhAkU4k3nOqBz9mNbvJ9EwMoQ although it exists and the path is correct.
I use ASP.NET C#, OS: Windows Server 2012 And IIS 8.0.
And error:
Help me... Please!
I want to forward you to an other question: What file extensions are blocked by default in IIS
The issue is that IIS has a configuration, how to handle requested files.
Depending on the extension, there have to be done different actions: While resources like .txt or .jpg files are just sent into the network as they are stored on the disk, other files like .aspx or .asmx have to be parsed, executed in dotnet and then the output have to be sent etc.
Other extensions like .dll or .exe are blocked by default, I think.
The problem is, that your files do not have any extension, so for this directory you have to configure that IIS should pass through all of the files.
Found another usefull link to microsoft: https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/fileextensions/

How to give a folder path in app.config file in C#

I am creating an application in WPF.
In that I am using XML file to store some settings.
My app will run for every 10 sec. So it will use that XML file settings.
My issue is in My local system i am calling the XML file as D://Foldername/projectname/test.xml .
But after deployment it is storing in C://Programfiles/Projectname/test.xml .
So how to give a generic path so that it runs in all the client systems.
I am creating setup file to install in clients systems.
Please help me.
Open the project properties page.
Click on Settings tab.
Add a new item called "MyPath". Make it an Application Setting of type String and give it a sensible default path name as value.
Reference the value in code with Properties.Settings.Default.MyPath.
If you open the applications config there will be a setting called MyPath where you can override the path at runtime.
I suggest you to put the XML file in the same folder as your EXE file and then use Assembly to get its current path.
var cfgPath = Assembly.GetExecutingAssembly().Location + ".config"
Update
it's better to name your config file the same with your exe file but with ".config" extension.
If you are really using ClickOnce, I hardly recommend you to create your own directory for data and configuration files:
private static string GetDataDir()
{
var dataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"YourApplicationName");
if (!Directory.Exists(dataDir))
Directory.CreateDirectory(dataDir);
return dataDir;
}
The problem with storing the data in the directory of the executable is, that it will be at a different location. While debugging, it will be in you \bin directory. When the application is deployed by ClickOnce, you gonna have a bad time. The installation directory for a ClickOnce application is created for every version. So if you EVER update your application at "customers", all their settings will be lost.

Read from output file in installed WCF service

I included a text file as output in a WCF set up project.
The text file is correctly located in the same folder with the dll, exe, and config file after the project is installed (C:\Program Files\KLTesting\KLTestApp).
However when the program tries to read it, it looks under "C:\Windows\system32", what's the correct way to find & read it?
I have
string a = Directory.GetCurrentDirectory();
a += "/R0303.txt";
string content = File.ReadAllText(a);
Thanks.
You should use AppDomain.CurrentDomain.BaseDirectory or AppDomain.CurrentDomain.SetupInformation.ApplicationBase instead, to get the Directory of your .exe file.
First you should not call Directory.GetCurrentDirectory() and concatenate with the file name. The service is running within a web server like IIS or some other type of container, so GetCurrentDirectory will not give you what you are thinking as you found out. (On a quick tangent, as a recommendation in the future if you want to do path combining you should use Path.Combine method as it will handle all the corner cases and be cross platform.)
There are a few ways to do this:
Assembly myAssembly = Assembly.GetAssembly(SomeTypeInAssm.GetType());
string fullFileName = Path.Combine(myAssembly.Location, "MyFile.txt");
The fullFileName should be what you are looking for. Make sure you use a type that is actually in an assembly located and referenced from the directory in question. However be aware because this file in your question in the Program Files area this is a secure area on Vista and higher OS's and you may not have access to do anything but read the file.
Also you could have the installer of the application place in the registry the install path. Then your service if on the same box can pull this information.
Also you could make sure the file you want to load is within the web server environment that is accessable to the WCF service in question.

Categories

Resources