C#访问路径被拒绝... Client.Download(filelink,path)

时间:2012-10-04 15:15:27

标签: c# asp.net download webclient directory-permissions

大家。首先:抱歉我的英语不好,我是意大利人......

我的代码存在问题: 当我尝试从dropbox下载文件,并将其放入我的漫游文件夹时出现问题,这里出现错误日志:

System.Net.WebException: An exception occurred during a WebClient request. --->              System.UnauthorizedAccessException: Access to the path'C:\Users\Marco\AppData\Roaming\.maycraft' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   --- End of inner exception stack trace ---
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at MayCraftLauncher.Program.downloadExist() in c:\Users\Marco\Desktop\MayCraftLauncher\C# SourceCode\MayCraftLauncher\MayCraftLauncher\Program.cs:line 71

显然,我尝试使用管理员权限运行Visual Studio,但没有...

以下是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Security.Principal;
using System.IO;
using System.IO.Compression;
using Ionic.Zip;

namespace MayCraftLauncher
{
    class Program
    {

        public static System.Threading.Thread existDownloading = new System.Threading.Thread(downloadExist);

        public static string userName = Environment.UserName;
        public static string path = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft\Exist";
        public static string maypath = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft";

        static void Main(string[] args)
        {
            folderExist();
        }

        static void check1()
        {
            while (!Directory.Exists(maypath))
            {
                System.Threading.Thread.Sleep(5000);
            }

                existDownloading.Start();
        }

        static void folderExist()
        {
            if (!Directory.Exists(maypath))
            {
                System.Diagnostics.Process.Start(@"C:\Users\Marco\Desktop\MayCraftLauncher.jar");
                check1();
            }
            else
            {
                fileExist();
            }
        }

        static void fileExist()
        {
            if (File.Exists(path)) //Determina se è la prima volta che l'applicazione viene eseguita
            {
                Console.WriteLine("Exist");
                Console.ReadLine();
            }
            else
            {
                existDownloading.Start();
                Console.WriteLine("Downloading...");
            }
        }

        static void downloadExist()
        {
            WebClient Client = new WebClient();
            DateTime btime = DateTime.Now;
            path = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft\";
            string publicLinkExist = @"https://dl.dropbox.com/u/35356155/minecraft/download/Exist";
            //string publicLinkMods = @"https://dl.dropbox.com/u/35356155/minecraft/download/mods.7z";
            string publicLinkMods = @"https://dl.dropbox.com/u/35356155/minecraft/download/mcp.7z";
            try
            {
                //----------------File downloading-------------------
                Client.DownloadFile(publicLinkExist, path);
                DirectoryInfo dev = Directory.CreateDirectory(path);
                string devpath = @"C:\Users\" + userName + @"\AppData\Roaming\.maycraft";
                Client.DownloadFile(publicLinkMods, devpath);
                DateTime atime = DateTime.Now;
                Console.WriteLine("Downloading time: " + atime.Subtract(btime).TotalSeconds + " sec.");

                //---------------Files Extracion-------------------
                using (ZipFile zip = ZipFile.Read(devpath))
                {
                    zip.ExtractAll(path);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                System.IO.File.WriteAllText(@"C:\Users\Marco\Desktop\log.txt", e.ToString());
                Console.ReadKey();
            }
            existDownloading.Abort();
        }


    }
}

我希望你能帮助我。先谢谢你!

0 个答案:

没有答案