C# .NET Trying to get a path of a project resource file - c#

This is my first post because I could not find any solution for my problem. Neither for myself nor in any thread.
Here is the situation. I do have a .JSON file which stores my credentials for an API authentification.
The software is used on multiple computers, so please do not propose anything regarding a hardcoded path.
My code:
string path = Path.GetTempPath();
path = path + "unterschrift.png";
if (System.IO.File.Exists(path) == false)
{
Properties.Resources.credentials.Save(path);
}
Now I do get the error: byte[] does not contain a definition for "Save"
Looking for your support!
The same code worked for an image file but not now because it is a different file format. I have already tried decoding the byte but it does not work.

Related

Saving byte array to file

I have a byte array and need to save it to a file.
I have tried the below code:
File.WriteAllBytes("form.txt", byteArray);
string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
I have referred https://stackoverflow.com/a/19455387/15265496 for the implementation.
I am looking for the file in Android emulator. Where will the file get saved from first line?
Should I create a form.txt in the application local folder?
Is there any alternative way to do the same?
You can find the appropriate folder using Environment.SpecialFolder.LocalApplicationData
string fileExactLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
You can find more information at https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows
You cannot access the file system of an Android emulator instance via explorer or finder.
As per the Microsoft docs, if you want to store a file in Android, you can always use the application's files folder which is accessible through:
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
A file saved to that folder will go to
/data/user/0/com.companyname/files

How can I access the filepath to Resources (iOS) folder in Xamarin.Forms?

I have a .sqlite file in the Resources folder of the iOS part of my app. In AppDelegate.cs right now I have:
string dbname = "test.sqlite";
string folderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Resources), "..", "Library");
string fullPath = Path.Combine(folderPath, dbname);
My issue with this is that when I load up the sqlite file using the sqlite-net-pcl library it shows an empty database, despite there being over 10,000 rows in the sqlite file. I think the issue is I'm linking to the wrong thing.
Once again, the sqlite file is in the Resources folder. How can I access its filepath?
Hallelujah I figured it out!
NSBundle.MainBundle.PathForResource("test", "sqlite");
Returns the filepath for the resource (at least it works on my simulator). I'm going to test this on an actual device and I'll post the results, but for now it's working. Essentially the above command searches for the specified resource, with the first string being the name and the second string being the extension, in this case sqlite.

How can i check the assembly version of a dll file that is stored on one of my iis websites and not on my local machine?

I have a dll file that iv'e put on my IIS website. Is it possible to check the assembly version from this file. I know how to do this if the file was stored on my local machine but its not. this is the code i tried:
string FilePath = "";
FileVersionInfo VersionInfo = FileVersionInfo.GetVersionInfo(FilePath);
string productVersion = VersionInfo.FileVersion;
The problem i'm getting is that when i enter a website directory in my FilePath variable it errors and says you can't enter a website path. Is there another way to use a website as a path and not a directory from my c drive??
If it is possible please give me a starting point and some websites to learn this from or some sample code. Would be much appreciated.
URL <> FileSpec
Thanks for telling us what you've tried. Sounds like you are trying to reference the DLL by the URL, not the file spec. A path like this:
http://MyNetworkWebServer/assy.dll
goes thru IIS and HTTP which intentionally bars actually viewing the dll for many reasons - the dll will be coded to serve up some content maybe but certainly not allow you to examine the bits.
A path like this:
\\MyNetworkServer\IisFileShare\assy.dll
goes thru the filesystem / filesharing areas of the server and will allow you to call those functions if proper permissions and security settings are in place.

Could not find a part of the path error on file download

I've taken on an asp/c# web app to fix originally done by the previous developer at my workplace. The code shows a Gridview populated by results from a query showing a list of files, one column is made up of 'command fields' that when clicked download a file. Everything seems to go smoothly until it reaches the file download as it can't seem to find the file on the server. My C# really isn't strong so bear with me and if you need further info that I've missed out please do say so.
Here is the specific part of code that causes problems:
//strSuppDocName - is already declared elsewhere
string path = System.IO.Path.Combine(Server.MapPath("~/Documents/"), strSuppDocName);
if (!Directory.Exists(path)){
System.Windows.Forms.MessageBox.Show(path + " - file path doesn't exist");
}
else {
System.Net.WebClient client = new System.Net.WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ClearContent();
Response.ClearHeaders();
FileInfo file = new FileInfo(path);
Response.Clear();
Response.AddHeader("Content-Disposition", "Attachment;FileName:" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(strExtSuppDoc.ToLower());
Response.WriteFile(file.FullName);
Response.End();
}
}
What happens when I run the code is that the grid view populates okay, I click the file to download and it enters the first branch of the if statement showing the path. Before I added in the if statement it was showing the following error: "could not find a part of the path". I've tried fiddling with the path such as setting it absolutely:
string path = System.IO.Path.Combine(#"E:\web\Attestation\Documents\", strSuppDocName);
And without using the Combine method above and using standard string concatenation with '+'. Any help or guidance is most appreciated, thanks!
You're mixing a handful of technologies here. First of all, this doesn't belong in a web application:
System.Windows.Forms.MessageBox.Show(path + " - file path doesn't exist");
Web applications aren't Windows Forms applications. This won't display anything to someone using the web application, because there's no concept of a "message box" over HTTP.
More to the point, however, you're using path in two very different ways. Here:
Byte[] buffer = client.DownloadData(path);
and here:
FileInfo file = new FileInfo(path);
Is path a URL on the network or a file on the file system? It can't be both. The first line is treating it as a URL, trying to download it from a web server. The second line is treating it as a local file, trying to read it from the file system.
What is path and how are you looking to access it? If it's a URL, download it with the WebClient and stream it to the user. If it's a file, read it from the file system and stream it to the user. You can't do both at the same time.
If you are interacting with a path on a network (aka UNC path), you have to use Server.MapPath to turn a UNC path or virtual path into a physical path that .NET can understand. So anytime you're opening files, creating, updating and deleting files, opening directories and deleting directories on a network path, use Server.MapPath.
Example:
System.IO.Directory.CreateDirectory(Server.MapPath("\\server\path"));
The answer in short is that the file name was incorrect.
Strangely or mistakenly the author of the code, when uploading a given file, added an extra extension so a file would be something like 'image.png' to start off with then when uploaded would become image.png.png. Why didn't I notice this before you may ask? Simply because the whole path wasn't shown in Windows XP (don't ask why I was using XP) when viewing it through the explorer window and I dismissed this issue long before - a big mistake! After trying to find the file by typing the address of the file into the windows explorer address bar and receiving an error that the file doesn't exist, yet I could plainly see it did, a colleague looked at for the file remotely using Windows 7 and we saw that the file was shown as 'image.png.png'. Thereafter the path to the file worked correctly.

C# System.Net WebClient.DownloadData - Error retrieving proper data

Perhaps the main issue is where I am uploading to - I am currently using MediaFire to upload my files. I have tested with downloading files of both ".exe" as well as ".png" formats; neither seem to work for me though.
The issue that is constantly occurring for me:
When I attempt to download a file, (I will put the URL's of the two files at the end of my question), the amount of data retrieved is either far greater - or far less than the actual size of the file. For example, I uploaded a blank VB6 executable file which is 16kb. The downloaded file comes out to be nearly 60 kilobytes!
Some things to note:
1) Both files download with no problems through Chrome (and I'm assuming other browsers as well).
2) I have tried multiple methods of retrieving the data from the downloaded file (same result).
My Code:
// Create a new instance of the System.Net 'WebClient'
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
// PNG File
string url = #"http://www.mediafire.com/imageview.php?quickkey=a16mo8gm03fv1d9&thumb=4";
// EXE File (blank VB6 exe file # 16kb)
// string url = #"http://www.mediafire.com/download.php?nn1cupi7j5ia7cb";
// Destination
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + #"\Test.png";
byte[] result = client.DownloadData(url);
MessageBox.Show(result.Length.ToString()); // Returns 57,000 something (over 3x larger than my EXE file!).
// Write downloaded data to desired destination
System.IO.File.WriteAllBytes(savePath, result);

Categories

Resources