1

i am trying to create a folder in the downloads folder using uwp, and in this folder i want to create pdf files. this is what is supposed to happen: the first time the button is clicked, the folder is created and so is a pdf file corresponding to a client. on the second button click, the folder must be checked to be existed, and so only the pdf file should be created inside it. the thing is that my code, without the part where it checks for folder existence, works on the first click, but doesn't work on the second because i get an exception that the folder already exists. but with the 'if' part, it doesn't work at all. like nothing is created. here is my code:

[assembly: Dependency(typeof(getpathUWP))]
namespace ALNahrainAlphaApp.UWP
{
    public class getpathUWP : path
    {
        public Task< string> get_path(string foldername, string filename, byte[] ar)
        {
            Task<string> t = Task.Run(() => pathtoget(foldername,filename,ar));

            return t;
           

        }

       

        async private Task<string> pathtoget(string foldername, string filename, byte[] ar )
        {
            //  StorageFolder newFolder = null;
          
            if (!File.Exists(@"C:\Users\ALNOOR\Downloads\d98cfcb0-e3cb-48e3-b720-fd9ace0ca7e8_htzz2mrv9gx22!App\alnahrainfiles"))
            {
                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"C:\Users\ALNOOR\Downloads\d98cfcb0-e3cb-48e3-b720-fd9ace0ca7e8_htzz2mrv9gx22!App\alnahrainfiles");
                StorageFile file = await folder.CreateFileAsync(filename);
                Stream stream = await file.OpenStreamForWriteAsync();
                stream.Write(ar, 0, ar.Length);
                stream.Flush();
            }
               
           

              
            else 
            {
                StorageFolder newFolder = await DownloadsFolder.CreateFolderAsync(foldername);

                StorageFile file = await newFolder.CreateFileAsync(filename);
                Stream stream = await file.OpenStreamForWriteAsync();
                stream.Write(ar, 0, ar.Length);
                stream.Flush();
            }

            return "";
           
        }
        }
}

note that i am using a dependency service. i tried other ways to check if the folder exists, but nothing is working. what am i doing wrong?

rana hd
  • 355
  • 3
  • 18

2 Answers2

1

this will open the folder if it exists, or create it and open it if it does not

StorageFolder newFolder = await DownloadsFolder.CreateFolderAsync(foldername, CreationCollisionOption.OpenIfExists);
Jason
  • 86,222
  • 15
  • 131
  • 146
  • thanks a lot for your reply. i tried it, unfortunately it didn't work – rana hd Nov 12 '22 at 05:05
  • “Didn’t work” is not a helpful description of the problem. What specifically is happening (or not happening)? Are you getting errors or exceptions? Have you stepped through the code in the debugger to verify exactly what is happening? – Jason Nov 12 '22 at 05:21
  • Yes, you're right I 'm sorry. Actually nothing happened, no folder or pdf file was created, no errors or exceptions, nothing happened. I put a break point in the code and ran it but it didn't stop at it maybe because it's a dependency service not the main code – rana hd Nov 12 '22 at 08:40
  • @ranahd - you need to add to question your revised code. Using the line in this answer, **do not** have any `if` test. That test is what makes "nothing happen" on the second call! – ToolmakerSteve Nov 12 '22 at 22:10
0

The File.Exists method is to check if the specified file exists. You said you need to check if the folder exists, you can use the Directory.Exists method to determine if the directory exists.

Zack
  • 1,255
  • 1
  • 2
  • 5