用C sharp写一个文件下载的例子
时间: 2023-12-02 11:05:53 浏览: 44
以下是一个使用C#编写的简单文件下载示例:
```csharp
using System;
using System.Net;
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
string url = "https://example.com/file.txt";
string filePath = @"C:\Downloads\file.txt";
try
{
client.DownloadFile(url, filePath);
Console.WriteLine("File downloaded successfully.");
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
```
在此示例中,我们使用`WebClient`类从指定的URL下载文件,并将其保存到本地计算机上的指定路径中。如果成功下载文件,则输出“File downloaded successfully.”,否则输出错误消息。
相关问题
用c sharp写一个webapi,提供文件下载功能
好的,您可以参考以下步骤:
1. 创建一个 C# WebAPI 项目。
2. 在 WebAPI 控制器中定义一个 GET 方法,该方法接受文件名作为参数。
3. 在 GET 方法中,使用 System.IO.File 类获取要下载的文件的物理路径,然后使用 System.Net.Http.HttpResponseMessage 类对其进行封装。
4. 在 HttpResponseMessage 对象的 Content 属性中,使用 System.Net.Http.StreamContent 类创建一个可将文件发送给客户端的流。
5. 将 HttpResponseMessage 对象返回给客户端。
以下是示例代码:
```csharp
using System.IO;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class FileController : ApiController
{
public HttpResponseMessage Get(string filename)
{
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
string filePath = HttpContext.Current.Server.MapPath("~/App_Data/" + filename);
if (!File.Exists(filePath))
{
httpResponseMessage.StatusCode = HttpStatusCode.NotFound;
httpResponseMessage.Content = new StringContent("File Not Found");
return httpResponseMessage;
}
FileStream filestream = File.Open(filePath, FileMode.Open);
httpResponseMessage.Content = new StreamContent(filestream);
httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = filename;
httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
return httpResponseMessage;
}
}
```
注意:此示例中的文件存储在 WebAPI 项目中的 App_Data 文件夹中。调用该方法时,将文件名传递给该方法即可下载文件。例如,使用 http://localhost/api/file?filename=test.txt URL 下载名为 test.txt 的文件。
用c sharp写一个数据保存
的程序要怎么写?
可以使用C#中的文件读写操作来实现数据保存的功能。首先,需要使用StreamReader或FileStream类读取或创建一个存储数据的文件,然后使用StreamWriter将数据写入文件中。具体实现方式可参考以下代码段:
```csharp
using System;
using System.IO;
namespace DataStorageDemo
{
class Program
{
static void Main(string[] args)
{
// 指定文件路径
string filePath = "data.txt";
// 写入数据
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.WriteLine("Hello World!");
sw.WriteLine("这是一条测试数据。");
sw.WriteLine("2021-01-01");
}
// 读取数据
using (StreamReader sr = new StreamReader(filePath))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
Console.WriteLine(line);
}
}
Console.ReadKey();
}
}
}
```
以上代码会在程序所在目录下创建一个名为"data.txt"的文件,并向其中写入3行数据。然后再从文件中读取数据并输出到控制台。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)