Package.Current.InstalledLocation.GetFolderAsync can't get the new folder under Assets - c#

I'm building a Windows 8 app. This app has an item page with a list of files, and when one of these files is selected, the app should open that file.
With these analysis I started to produce code but immediately I was confronted with the difficulty to open a directory that contains a list of files in windows 8 app.
In particular I created a directory under Assets with name Report that contains the files list (pdf, txt, any kind of files).
When I need to open this directory programmatically, I'am not able to open it because I get this exception:
System.IO.FileNotFoundException
but the directory exists. I try to open it with:
var folder = await StorageFolder.GetFolderFromPathAsync(Windows.ApplicationModel.Package.Current.InstalledLocation.Path + #"\Assets\AnimalImages");
as Xyroid suggests here enter link description here but I have fix nothing.
In another attempt, I tried to access at that directory using that code:
IReadOnlyList<StorageFolder> documentsFolder = await Windows.ApplicationData.Package.Current.InstalledLocation.GetFoldersAsync();
foreach(StorageFolder folder in documentFolder){
IReadOnlyList<IStorageItem> subItems = await folder.GetItemsAsync();
foreach (IStorageItem subFolder in subItems)
{
try
{
...
}
catch(FileNotFoundException e1) { }
}
}
In this second approach, I tried to get the Assets folder (with the first loop), and I got it, and the report folder (with the second loop), that I'm not able to get it because the directory named reports does not exists.
Please, anyone of you know C# and windows 8 app better than me.
i look forward to hearing from you.
Thank you for time you reserved me.

Related

How to create a content file and navigate into it from a console application?

Im working on a "text only" game with console application. I searched for ways to add sound to my application, but all of the solutions needed to include a path to the .wav file location. that would work, but I want to publish my game later on. and the file location matches only my PC, I mean, the user who downloads it can put the content file in a diffrent location than c:/Mygame/Content. what if he has multiple hard-drives? or a Disk on key? the program wont play the sound, because it cant find the sound. any ideas?
Store the WAV file in a location relative to your .exe file and use:
System.Reflection.Assembly.GetExecutingAssembly().Location
to dynamically get the location of your .exe file at runtime and append it to the name of the .WAV file.
I solved it myself with these lines of code:
` string exe_location = System.Reflection.Assembly.GetExecutingAssembly().Location;
exe_location = exe_location.Remove(exe_location.Length - 11);
int last_path_chat = exe_location.Length;
string sound_location = exe_location.Insert(last_path_chat, "DIE_ANTWOORD_-_UGLY_BOY.wav");
SoundPlayer.SoundLocation = sound_location;
SoundPlayer.PlaySync();
You should be able to have your console application reference the .wav format file through a relative path(something like "~/Content", and have it called in reference to the current directory of the application. Once that's done, you could build a simple installer(or just have the .wav file site in the program directory if you'll just be deploying your app via copy) to deploy your app.
I'm at work right now so can't do any quick testing, but if this question is still outstanding when I get home tonight I'll mock something up and post it here.

How can I get the file path for a file I only know the name of? C#

So I'm making a Tic Tac Toe application and have created a Text file linked to the program to hold the following information:
the name
the time took to win
the difficulty
I know the timer is redundant for a quick game like Tic Tac Toe but I'll use it in the future for other programs.
My question is how can I find the full path of the file while only knowing the name of the file?
I want to do this using the program so it can be transferred to any computer and still be able to access the file without the user having to input it.
The code I've tried is:
string file_name = Path.Combine(Environment.CurrentDirectory, "Tic Tac Toe\\HighScores.txt");
But this just looks in the Debug folder, where the file isn't located. The application is entirely a console application.
Try to dedicate the file in a fixed sub directory:
\TicTacToe.exe
\settings\settings.cfg
So the path is dependent of your executable file.
You'll fetch the directory by calling Directory.GetCurrentDirectory()
You can set a desired directory by setting Environment.CurrentDirectory
A common way to handle this case is the one described above.
Another would be to use user specifiy directories like the %appdata% path and create a dedicated directory there.
%appdata%\TicTacToe\settings.cfg
Everytime your application starts it should lookup the folder %appdata%\TicTacToe\
If it is present, your application has been executed with this user.
If not, just create a new one, so we know it's the first run.
You can get the %appdata% path by calling
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Example of what i would have done
private void setUp(){
string filename = "settings.cfg";
string dir = "TicTacToe";
string appdata =Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fullpath = Path.Combine(Path.Combine(appdata,dir),filename);
//check if file exists, more accurate than just looking for the folder
if(File.Exists(fullpath )){
//read the file and process its content
}else{
Directory.CreateDirectory(Path.Combine(appdata,dir)); // will do nothing if directory exists, but then we have a bug: no file, but directory available
using (FileStream fs = File.Create(fullpath))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
}
Hope it helped.
Perhaps have a configuration file for your application and store the directory name in there.
An old example from MS, but should still be applicable...
How to store and retrieve custom information from an application configuration file by using Visual C#

Google Drive API: I have to fetch only files in that folder without trashed files

I am trying to get a list of files in a folder on Google drive using the Google drive API. When I make my request I am getting a list of child elements in that folder including previous deleted files.
How do I get only the files that are in the folder without the ones that have been deleted or trashed?
I don't think you are seeing deleted files. Deleted files are permanently deleted. I think you are seeing trashed files which are like putting files in the trash can on windows, they are removed but not really gone.
You want to add a search parameter to your files.list request. you want to tell it to not include trashed.
trashed = false
I found the issue and i added this piece of code to resolve the issue,
Google.Apis.Drive.v2.Data.File fileDetails = new Google.Apis.Drive.v2.Data.File();
try
{
fileDetails = service.Files.Get(fileId).Fetch();
//check whether files is in thrash and ignore if any
if (fileDetails.Labels.Trashed.GetValueOrDefault())
{
return null;
}
}

Zipping files not appearing in correct location

I'm new to C# and would appreciate a little help.
I've a windows console application that emails logs and is scheduled to run every hour.
What I want is to zip those logs(after they have been emailed) in the same folder. ie. the folder the application is reading the logs from.
This is snipped of my code so far.
string[] files = Directory.GetFiles(#"C:\Users\*\Documents\target", "*.txt");
try
{
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) //i'm using dotnetzip lib
{
foreach (var file in files)
{
Console.WriteLine(file);
sendEMail(file);
zip.AddFile(file,"logs");
}
zip.Save("mailedFiles.zip");
}
}
What's happening with the above code is I'm able to create a zip file but not in the same folder where the application is reading from. Instead it creates the zipfile in my program's location(which makes sense).
How do I go about changing the location of the created zipfile. Also I want the individual logs to be replaced by the one zipfile that's created.
You can save the zip file to any of the special folders available in the user folder. You can get paths for the special folders with the following line of code:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
If you want to save the zip file other than these special folders then there might be permission issues.
Try saving it by using the directory.
For example:
{
string directory = #C:\Users*\Documents\
zip.Save(directory + "mailedFiles.zip");
}
You should also use System.IO to get the directories instead of hardcoding them.

Access to the path .... is denied

I have created a .msi by using VS2008 setup project. My application frequently writes some value in a .txt file in the application directory (C:\Program Files\MyApp\MyFile.txt). After insalling it in Win7, it raises an exception "Access to the path .... is denied."
But whenever I run it as administrator, no such exception occurs. Here is my sscce
string FilePath=Application.StartupPath + #"\AppSettings\CurrentUserName.inf";
using (StreamWriter writer=new StreamWriter(FilePath,false))
{
writer.Write(txtLoginName.Text.Trim());
}
MainForm.ProcessLogIn();
this.DialogResult = DialogResult.OK;
I don't know how to solve this problem. Any suggestion?
Move your file out of Program Files directory. In Win7 is readonly for normal users.
You could move the file in the ProgramData directory.
Your installer should create a directory for your application there.
Then inside your code you could retrieve the correct full pathname using these lines of code
string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
string appFile = Path.Combine(dataPath, "MyAppDir", "MyFile.txt");
usually (on Win7) this result in a path like this
c:\programdata\MyAppDir\MyFile.txt
but using the SpecialFolder enum you are guaranteed to use a folder available in readwrite to your application not depending on the current operating system.
The only way to solve this problem is to not write to that folder. You are not allowed to write to that folder by convention, unfortunately, older versions of Windows did not hold you to this.
Instead, you can use Environment.SpecialFolder to help you find where you need to go:
// your application data for just that User running the app
var perUserAppData = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData);
// your application data for ALL users running the app
var allUsersAppData = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData);
// better!
var path = Path.Combine(perUserAppData, #"MyApp\MyFile.txt");
Basically, Windows 7 is telling you that you're going to have to stop driving on the sidewalks and use the street as was intended.
As a short-term fix, you can use ICACLS to grant write access to the file. Note: NOT the whole directory.
As a longer term fix, you should NOT write to the program directory if you are running as unprivileged users, but instead somewhere like %LOCALAPPDATA% or %APPDATA%.

Categories

Resources