System.IO, where do the files get saved? - c#

i am saving a File in Xamarin Forms using System.IO and i am not able to find it in the File-Explorer of the device, when debugging it on Live Player.
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "test.txt");
File.WriteAllText(path, "test12345678");
It throws no exception, and i can read the file afterwards using File.ReadAllText
I would expect finding this file in the Documents folder in the explorer, but i can not find it there.
Does Xamarin somehow encapsulate this from the device, or is it in some hidden Directory, which i am not able to see?

Related

How to get the path of the uploaded file within the AppBundle?

We are using Autodesk Forge's Design Automation API. We have an AppBundle ready and we put an .rfa file into the same folder which contains the .dll file. When the AppBundle is unzipped on the Forge servers, which path can lead to our .rfa file? (how can we access it?) Our goal is to place the attached Family file's contents into the input file which is being uploaded with the API, and the result should be a new file which contains the additions from the file which we uploaded within the AppBundle. The process works when testing with Revit locally, but it doesn't work with the API. In the report we are retrieving it's obviously pointing out that the attached file cannot be found:
Autodesk.Revit.Exceptions.FileNotFoundException: T:\Aces\Jobs\ced628d35ecf4412b68c024e2cec098b\something.rfa
On the code side, we are trying to access the .rfa file via this path:
static string currentDir = Environment.CurrentDirectory;
static string path_input = currentDir+#"\something.rfa";
This seemed as a logical path, but as it turned out, it's not..
Is there a way to access the .rfa file inside the uploaded AppBundle?
I took a look at the Restrictions but reading the file from the AppBundle is not mentioned as restricted or not approachable. Am I missing something?
A .NET assembly knows its own path. You can call System.Reflection.Assembly.GetExecutingAssembly().Location within it to find the current path of the dll. You can then compute the path of the .rfa file relative to the folder of the dll and use it / open it. Thus you should be able to open any file you package along with your addin in your appbundle.
You can simply modify your code to:
static string assemblyLocation = Assembly.GetExecutingAssembly().Location;
static string assemblyDirectory = Path.GetDirectoryName(assemblyLocation);
static string path_input = assemblyDirectory+#"\something.rfa";
One thing to note however, is that you only have readonly access to files in appbundle. If your code relies on modifying these during execution, then you may simply copy the source rfa file to the current working folder and then work with the copied file instead.
Also see more details in blog for similar ideas.
We have a blog post on how you can either pass in the app bundle path in the commandLine parameter or find the path via the location of the add-in dll:
https://forge.autodesk.com/blog/store-template-documents-appbundle

How to read and write a txt file outside of project folder (Unity)

I tried using File, StreamWriter and StreamReader with a path outside of my project folder but it didn't work. It became projectPath\externalPath. For example my project path is F:\Project\ and my txt file is in D:\File.txt, Unity automatically read my path as "F:\Project\D:\File.txt", or it gives an error message: UnauthorizedAccessException
I tried using WWW too, but I got an error message "cannot convert WWW to string"
please help
Try giving an absolute path instead of a relative path.
e.g
var fullPath = "D:\<file-name>.txt";
var content = "testing";
File.WriteAllText(fullPath, content );
Above should write the file in your "D:" drive.
After looking into various forums, I finally found my answer.
Turns out you have to use an absolute path (like RSF said in the comments below) and uncheck the "read only" checkbox in the folder properties to avoid unauthorized access exception when you want to write into the 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.

Can't extract .zip file. Getting Invalid Data Exception

I have a very large zip file which contains other zip files inside of it. I want my c# program to be able to recognize that the file is a zip file and if it is a zip file, then to extract it to a folder in the same location as the zip file. My code is here:
private void Unzip(OpenFileDialog tvZipOpen)
{
string zipFile = tvZipOpen.FileName; // file to unzip
int i = zipFile.LastIndexOf(".zip");
string targetDirectory = zipFile.Substring(0, i); // location to extract to
using (ZipArchive zip = ZipFile.OpenRead(zipFile))
{
zip.ExtractToDirectory(targetDirectory);
}
tvZipOpen.InitialDirectory = targetDirectory;
tvZipOpen.ShowDialog();
}
I am using the ZipFile class from .NET 4.5 and i call on this method here:
if (tvOpen.ShowDialog() == DialogResult.OK)
{
while (tvOpen.FileName.ToLower().EndsWith(".zip"))
{
Unzip(tvOpen);
}
return tvOpen.FileNames;
}
The code works fine for extracting the first zip file but when I try to extract the second zip file, I get an InvalidDataException that says local file header is corrupt. However, I don't think it is corrupt because I am able to open and extract the zip files perfectly in windows explorer. I'm not sure if the fact that it is a large zip file with a zip64 extension has anything to do with it but whatever the problem is, how come I don't get the problem when I open and extract in windows explorer and how do I fix this? Any help would be greatly appreciated.
c# does not support the .zip64 extension.
how large is your zip file because if it under 4GiB rename it to .zip and it should work fine if it is larger than that see this
http://dotnetzip.codeplex.com/
To change the file extension
Open windows explorer and press Alt + V
Then go to tools and then folder options and make sure that the hide extensions for known file types box is unchecked and click apply and ok.
then simply rename the file to remove the 64 from the extension so it is just .zip
Then Click yes on the prompt
And then you should be able to open the file in your program
hope this helps

UnauthorizedAccessException when trying to write to text file in C#

I'm trying to write a blank text file which is included within my installer but i'm getting the following error;
System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\Hex Technologies\wamplocation.txt' is denied.
It seems to be the permissions of the file once it's installed through my installer, but how can I set the file to be fully modifiable once the file installed?! Can this be done through C#?!
EDITTED;
wamp_url = openFileDialog1.FileName.ToString();
String EnviromentPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
StreamWriter outfile = new StreamWriter(EnviromentPath + #"\Hex Technologies\wamplocation.txt");
outfile.Write(wamp_url);
outfile.Close();
You should not store your modifyable data files in the Program Files path. Use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) or Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
The Program Files\... path is protected against modification by normal users on Win7+. It would be a bad idea to try to circumvent that protection.
The likleyhood is the UAC is getting in your way.
Ideally your program shouldn't be writing to this location, it this modification file is to be modified during an install process and nowhere else you need to make sure that you are running elevated.
If this file is to be modified at run time you should consider the use of either %appdata% for user data or %programdata% for program data instead of program files.

Categories

Resources