How to get to the parent directory of the app after drag'n'drop starting it? (C#) - path

I'm trying to make the app which is usually started by drag'n'dropping files on it to load the config file common to other apps, which resides in the parent directory of the app.
The problem is whenever I start the app by drag'n'dropping, #"..\config.cfg" beomes #"where\the\drag_n_dropped\file\is\..\config.cfg" and not the app's own directory.
How do I fix it?
Answer:
string pathToThisApp =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
and after getting the path we can extract the parent directory by using
pathToThisApp.Substring(0, pathToThisApp.LastIndexOf(#"\") + 1);
+ 1 is needed to keep the last \ sign in the path string.

Use Assembly.GetExecutingAssembly().Location

Related

cordova-plugin-file: files not accessible after app update (iOS)

I am experiencing a funny problem: I am developing an ionic app, using cordova-plugin-file to store images. The resulting paths (URIs in form file:///...) are stored in a SQLite DB along with more information. When I install the app and use it, all works perfect. But if I update (or reinstall) the app, the old images are not found anymore, while newly added images work perfect.
I first suspected that the image files were being deleted on update, but I checked the content of the directory and the files are still there. (FYI: I am using file.dataDirectory/scans/ to place my files).
Then I thought that maybe I could go around the problem loading into img src a base64 blob loaded with cordova.file.readAsUrl(), but cordova.file does not find the file as well (I insist, the files are there, I checked with XCode).
So I checked further with cordova.file and it only finds files that are added after last app install, but the older files are still present in the directory.
Here my code:
private getImgSrcFromDocument(doc: Document): any {
const uri = doc.fileName;
const src = this.webView.convertFileSrc(uri);
const sanitized = this.sanitizer.bypassSecurityTrustUrl(src);
console.log({uri, src, sanitized});
return sanitized;
}
<img class="document_thumbnail" [src]="getImgSrcFromDocument(doc)">
I have already checked this, but is not my case.
By the way, the same code works perfect on Android.
Any idea what could be the problem?
Thanks in advance!
I found the reason why and the solution (very obvious when you know the problem):
Reason
On every new install, iOS renames the data directory for the app. The directory path has this form:
file:///var/mobile/Containers/Data/Application/ABC0000-1234-99DD-00FA-E835FEA/Library/NoCloud/
The hash in the middle is renewed on every install, so the stored full paths in DB are not valid anymore.
Solution
If you still can do it (no deploy yet, no real users), store only the relative path and complete it every time with this.file.dataDirectory (or wherever you wanted to store your files).
If you already have real users and want your update to 'find the files', just ignore the first part of the stored path and build it like before:
const ValidUri = this.file.dataDirectory + // The injected cordova-plugin-file
'relativeSubDirectories/' + // If you store your files in some subdirectory
this.document.storedFullPath.substr( // Take from fullPath only the filename
this.document.storedFullPath.lastIndexOf('/') + 1
);
Where storedFullPath is the string file:///var/mobile/....
After that, you still have to do the webView conversion and the sanitizing, like in the question above.
Hope this helps someone.

Application private directory on iOS with access to creation of files from Qt APIs

I am using Qt app on iOS & want to save files into Application's private directory so that other apps cannot access my files. Reading through the documentation here, /Documents directory under data container looks to be the path to save my files which are private to app.
Firstly I chose to use QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) which returned
/var/mobile/Containers/Data/Application/123XX-XXXX-XXXX-XXXX/Library/Application Support/MyApp. But then I found that I could not create any files in there. Tried /Documents inside the App folder as well. But in vain.
Secondly I tried QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) which returned /var/mobile/Containers/Data/Application/123XX-XXXX-XXXX-XXXX/Documents. I am able to create & save my files here.
On iOS, which is the correct path to create files which are meant to be private to the application ?
How can I get the path using Qt?
The directory named with some ID as 123XX-XXXX-XXXX-XXXX, Is this the application directory or is it Library/Application Support/MyApp inside the same directory ?
I had same problem, for example I created a sqlite file. But the file that you will create can be every file you wants. In my example I need to create a sqlite file.
I solved like this:
QString dbName = "mydb.sql";
QString dbLocation;
#ifdef Q_OS_ANDROID
dbLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
#endif
#ifdef Q_OS_IOS
dbLocation = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#endif
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbLocation + "/" + dbName);
qDebug() << db.database(dbLocation + "/" + dbName);
qDebug() << db.databaseName();
Now you can save the file, I used DocumentsLocation to get a path, always works on iOS, but not work on Android, to be ready to Android you use another path: AppDataLocation.
In the documents location you can write/read/put and create files on iOS.
In the AppDataLocation location you can write/read/put and create files on Android.
Permission not denied in DocumentsLocation.

Save Lokijs DB in Electron

there is some way from inside the "main.js" electron to save a file out of the asar?
I'm fighting with this command to point the way out of the write-only area but I can not do it.
It would be nice that the path was inside /my-project/resources/ and would work even without the electron-package.
let configFilePath = `${__dirname}/../config.json`
db = new loki(configFilePath)
if(fs.existsSync(configFilePath))
db.loadDatabase()
Attempting to write a file within the application installation directory is a bad idea, often the user will not have the permission to do so. Instead you should write files to the location returned by app.getPath('userData').

Team Foundation Build Activitie "DownloadFiles" is giving error

I am customizing the default build process template in TFS 2010.
i am using "DownloadFiles" build activity and in server path i have given "$/TFS/Libraries/Foo.DLL", when i run the execute definition its throwing error as "Access to the path '\ServerName\SharedFolder\BuildName\TempFolder' is denied.".
But when i give server path as "$/TFS/Libraries" its downloading all the files in Libraries folder into shared TempFolder.
But i need do download only one file. Please help..
Thanks in advance..
Now, DownloadFiles does work for a whole folder only:
ServerPath="$/proj/path" - works great, all is downloaded to LocalPath.
ServerPath="$/proj/path/name.ext" - borked.
I've de-compiled DownloadFiles to see why: First it gets a list of server items, in our case just $/proj/path/name.ext. Then, it calculates the local path like this:
localItemPath = Path.Combine(LocalPath,VersionControlPath.MakeRelative(ServerItem, ServerPath));
In this line, the activity assumes that ServerPath is a path. If it's not, then MakeRelative will not recognize it, and the local path will be LocalPath/$/proj/path/name.ext, as the OP has observed.
Also, if ServerPath is not canonical - for example, $/proj/path/../path2, the same will happen. Solution: use VersionControlPath.GetFullPath(myNonCanonicalPath).
You need to grant the user running the build service with write permissions on the shared folder.
http://msdn.microsoft.com/en-us/library/cc668757.aspx
There are two separate Build activities, DownloadFiles for a folder ServerItem and a DownloadFile for a single file ServerItem.I'd expect it should work with DownloadFile.

system.io.directorynotfound -> But it works in Console

My files are referenced like so (it's all relative):
// WHERE YOU KEEP THE PAGE TITLE XML
public static string myPageTitleXML = "xml/pagetitles.xml";
and
using (StreamReader r = new StreamReader(myPageTitleXML))
{ //etc.. . .etc....etc..
}
I get system.io.directorynotfound, and "this problem needs to be shut down", when I double click the executable. But running it from the console works like a charm. What's wrong here?
I played around with attempting to set Environment.CurrentDirectory but couldn't get anything to work. Why should I have to do that anyway? It defeats the purpose of a relative path no?
responding.. .
"application" does not exist in the current context, i'll keep trying what people have mentioned, this is not a windows.form
testing
Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML); gives error URI formats are not supported, as does Path.GetFullPath(). Server.MapPath results in an error as well, this is currently offline
Well assuming this directory is somewhere under the directory in which your code is executing, it sounds like you can use ..
Application.ExecutablePath()
or
Application.StartUpPath()
.. to get an idea as to what your application is seeing when it goes in search of an 'xml' directory with the 'pagetitles.xml' file in it.
If the directory returned by one of these methods does not point where you thought it did, you'll need to move the location of your application or the location of this folder so that it is within the same directory as the app.
Hope this gets you on the right path.
So, when you run it from double clicking the executable, is there a file named pagetitles.xml in a folder named xml, where xml is a folder in the same location as the executable?
It's certainly possible to use relative paths like this, but I wouldn't really recommend it. Instead, maybe use something like:
string fileToOpen = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML);
using (StreamReader r = new StreamReader(fileToOpen))
{
//etc.. . .etc....etc..
}
Is this ASP.NET code? If so then you probably need to do MapPath("xml/pagetitles.xml")

Resources