/// <summary>
        /// Force load of storage file from telegram.
        /// Carefully, you can loose your unsaved data.
        /// </summary>
        public async Task ForceLoad()
        {
            using (await _saveLock.LockAsync())
            {
                var chat = await _botClient.GetChatAsync(_saveResChatId);

                if (chat.PinnedMessage?.Caption?.Trim() != FileResName)
                {
                    //Can't find storage.
                    File.WriteAllText(_storageFilePath, "{}");
                }
                else
                {
                    var fileId = chat.PinnedMessage.Document.FileId;
                    if (File.Exists(_storageFilePath))
                    {
                        File.Delete(_storageFilePath);
                    }
                    using (var stream = File.OpenWrite(_storageFilePath))
                    {
                        await _botClient.GetInfoAndDownloadFileAsync(fileId, stream);
                    }
                }

                LoadStorageStateFromFile();
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Opens the necessary files and handles a bunch of exceptions.
 /// </summary>
 /// <param name="bmp">Bitmap representing the input image</param>
 /// <param name="fstream">Output file stream</param>
 static string OpenFiles(out Bitmap bmp, out FStream fstream)
 {
     bmp     = null;
     fstream = null;
     try
     {
         bmp = (Bitmap)Bitmap.FromFile(inPath);
         if (grey)
         {
             bmp = ToGreyscale(bmp);
         }
         if (File.Exists(outPath))
         {
             File.Delete(outPath);
         }
         fstream = File.OpenWrite(outPath);
     }
     catch (OutOfMemoryException)
     {
         return("The file at \"" + inPath + "\" is not a valid BMP, GIF, JPEG, PNG, or TIFF format.");
     }
     catch (UnauthorizedAccessException)
     {
         return("You do not have access to \"" + outPath + "\".");
     }
     catch (System.IO.DirectoryNotFoundException)
     {
         return("The path: \"" + outPath + "\" is invalid. The directory does not exist.");
     }
     catch (System.IO.FileNotFoundException)
     {
         return("The path: \"" + inPath + "\" does not exist.");
     }
     catch (System.IO.PathTooLongException)
     {
         return("The path: \"" + outPath + "\" is too long.");
     }
     catch (Exception e)
     {
         if (e is ArgumentException || e is NotSupportedException)
         {
             return("The path: " + (bmp == null ? "\"" + inPath + "\"" : "\"" + outPath + "\"") +
                    " is invalid.");
         }
         throw;
     }
     return(string.Empty);
 }
Exemplo n.º 3
0
        /** This method is used by all code generators to create new output
         *  files. If the outputDir set by -o is not present it will be created.
         *  The final filename is sensitive to the output directory and
         *  the directory where the grammar file was found.  If -o is /tmp
         *  and the original grammar file was foo/t.g4 then output files
         *  go in /tmp/foo.
         *
         *  The output dir -o spec takes precedence if it's absolute.
         *  E.g., if the grammar file dir is absolute the output dir is given
         *  precedence. "-o /tmp /usr/lib/t.g4" results in "/tmp/T.java" as
         *  output (assuming t.g4 holds T.java).
         *
         *  If no -o is specified, then just write to the directory where the
         *  grammar file was found.
         *
         *  If outputDirectory==null then write a String.
         */
        public virtual TextWriter GetOutputFileWriter(Grammar g, string fileName)
        {
            if (outputDirectory == null)
            {
                return(new StringWriter());
            }

            // output directory is a function of where the grammar file lives
            // for subdir/T.g4, you get subdir here.  Well, depends on -o etc...
            string outputDir  = GetOutputDirectory(g.fileName);
            string outputFile = Path.Combine(outputDir, fileName);

            Directory.CreateDirectory(outputDir);

            return(new StreamWriter(File.OpenWrite(outputFile), Encoding.GetEncoding(grammarEncoding)));
        }