首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用C#以编程方式找到我的Google Drive文件夹?

如何使用C#以编程方式找到我的Google Drive文件夹?
EN

Stack Overflow用户
提问于 2012-08-29 15:44:47
回答 1查看 7.5K关注 0票数 22

here类似的问题。只针对Google Drive而不是Dropbox

如何使用C#以编程方式查找Google Drive文件夹?

  • Registry?
  • Environment Variable?
  • Etc...
EN

回答 1

Stack Overflow用户

发布于 2016-07-01 02:08:44

我接受了Sarath的回答,对它进行了修改,使其更具弹性(数据源路径周围的引号,以读者索引为条件的null条件,额外的错误检查,"using“以便适当地处理对象,添加了一堆注释,并添加了一些LINQ (因为,linq :-) )。

这个特定的实现捕获并记录异常,然后在我当前的应用程序需要的任何error...because上返回string.Empty。如果您的应用程序需要异常,请删除try/catch。

代码语言:javascript
复制
/// <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;
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12173077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档