2012-08-29 23 views
19

hereと同様の質問です。ちょうどGoogle Driveの代わりにDropboxCドライブでGoogleドライブのフォルダをプログラムで見つけるにはどうすればよいですか?

私はGoogle DriveフォルダをC#でプログラムで見つける方法を教えてください。

  • レジストリ?
  • 環境変数?
  • 等...
+0

私は、ユーザーの代わりにCの「Googleドライブ」を選択できるようにしたい:\ユーザーは、私のソフトウェアのインストール時に親フォルダとしてXY \ Googleドライブ\を\。私はレジストリとAppDataを検索しました。 %APPDATA%\ Local \ Google \ Driveにsync_config.dbがありますが、これをどのように処理するのか分かりません。 – wollnyst

+0

GoogleドライブAPIをお試しいただきましたか? https://developers.google.com/drive/quickstart – Surfbutler

+0

こちらをご覧ください:https://developers.google.com/drive/examples/dotnet – Ademar

答えて

14

私は個人的に考えて、最善の方法は、SQLite3のを通じて同じファイルにアクセスすることです。

string dbFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Google\\Drive\\sync_config.db"); 
if (!File.Exists(dbFilePath)) 
    dbFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Google\\Drive\\user_default\\sync_config.db"); 

string csGdrive = @"Data Source="+ dbFilePath + ";Version=3;New=False;Compress=True;";     
SQLiteConnection con = new SQLiteConnection(csGdrive); 
con.Open(); 
SQLiteCommand sqLitecmd = new SQLiteCommand(con); 

//To retrieve the folder use the following command text 
sqLitecmd.CommandText = "select * from data where entry_key='local_sync_root_path'"; 

SQLiteDataReader reader = sqLitecmd.ExecuteReader(); 
reader.Read(); 
//String retrieved is in the format "\\?\<path>" that's why I have used Substring function to extract the path alone. 
Console.WriteLine("Google Drive Folder: " + reader["data_value"].ToString().Substring(4)); 
con.Dispose(); 

hereから.NetのSQLiteライブラリを入手できます。 また、System.Data.SQLiteへの参照を追加し、上記のコードを実行するためにプロジェクトに追加します。

は、上記のコード

4

私は、Sarathの答えを取った(より弾力があるためにそれを作り直しからユーザー、relpace entry_key='user_email'を取得するには、読者のインデックス作成のヌル条件付きデータ・ソース・パス、周りの引用符、追加のエラーチェック、 "using"オブジェクトは適切に配置され、コメントが追加され、いくつかのLINQが投げられます(linq :-)のため)。

この特定の実装は、例外をキャッチしてログに記録し、エラーでstring.Emptyを返します。これは、現在のアプリケーションで必要とされるためです。あなたのアプリが例外を望むなら、try/catchを削除してください。

/// <summary> 
/// Retrieves the local Google Drive directory, if any. 
/// </summary> 
/// <returns>Directory, or string.Empty if it can't be found</returns> 
public static string GetGoogleDriveDirectory() 
{ 
    try 
    { 
     // Google Drive's sync database can be in a couple different locations. Go find it. 
     string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); 
     string dbName = "sync_config.db"; 
     var pathsToTry = new[] { @"Google\Drive\" + dbName, @"Google\Drive\user_default\"+ dbName }; 

     string syncDbPath = (from p in pathsToTry 
          where File.Exists(Path.Combine(appDataPath, p)) 
          select Path.Combine(appDataPath, p)) 
          .FirstOrDefault(); 
     if (syncDbPath == null) 
      throw new FileNotFoundException("Cannot find Google Drive sync database", dbName); 

     // Build the connection and sql command 
     string conString = string.Format(@"Data Source='{0}';Version=3;New=False;Compress=True;", syncDbPath); 
     using (var con = new SQLiteConnection(conString)) 
     using (var cmd = new SQLiteCommand("select * from data where entry_key='local_sync_root_path'", con)) 
     { 
      // Open the connection and execute the command 
      con.Open(); 
      var reader = cmd.ExecuteReader(); 
      reader.Read(); 

      // Extract the data from the reader 
      string path = reader["data_value"]?.ToString(); 
      if (string.IsNullOrWhiteSpace(path)) 
       throw new InvalidDataException("Cannot read 'local_sync_root_path' from Google Drive configuration db"); 

      // By default, the path will be prefixed with "\\?\" (unless another app has explicitly changed it). 
      // \\?\ indicates to Win32 that the filename may be longer than MAX_PATH (see MSDN). 
      // Parts of .NET (e.g. the File class) don't handle this very well, so remove this prefix. 
      if (path.StartsWith(@"\\?\")) 
       path = path.Substring(@"\\?\".Length); 

      return path; 
     } 
    } 
    catch (Exception ex) 
    { 
     Trace.TraceError("Cannot determine Google Drive location. Error {0} - {1}", ex.Message, ex.StackTrace); 
     return string.Empty; 
    } 
} 
関連する問題