Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to show the names of all the files in a specific directory in data grid in silverlight-4.0
and download that file.
Thanks in advance.
Posted
Updated 1-Dec-11 1:41am
v8
Comments
Reiss 28-Nov-11 6:49am    
What are you struggling with? Getting a list of file names or the data binding or something else?
dev.pratik 28-Nov-11 6:51am    
i can't get file list from directory :(

You can use
C#
string folderPath = ""; // 
string[] files = System.IO.Directory.GetFiles(folderPath);// this also accepts a pattern if you want to filter the files to a particular extension etc.
 
Share this answer
 
Here is the solution that i finally got and works successfully.. :)
C#
//in xaml.cs
public void BtnDownload_click(object sender, EventArgs e)
{
 var currentRow = dgDownloadFile.SelectedItem;
 string sFileName = currentRow.ToString();
 string sFullPath = "C:/FilesToDownload/" + sFileName;  //file path from where you want to download file

 Service1Client proxy = new Service1Client();

 proxy.DownloadFileCompleted += new ventHandler<DownloadFileCompletedEventArgs>(proxy_DownloadFileCompleted);

 proxy.DownloadFileAsync(sFullPath);  //wcf service client
}



void proxy_DownloadFileCompleted(object sender, DownloadFileCompletedEventArgs e)
{
 if (e.Result == 1)
   MessageBox.Show("Download successfully.");
 if(e.Result == 2)
   MessageBox.Show("File already exists.");
}




//in WcfService
[WebMethod]
public int DownloadFile(string FullPath)
{
 string[] fn = FullPath.Split('/');  //get your filename
 FileStream fileStream = null;
 BinaryReader reader = null;
 fileStream = new FileStream(FullPath, FileMode.Open, FileAccess.Read);
 reader = new BinaryReader(fileStream);
 byte[] fileeBytes = reader.ReadBytes((int)fileStream.Length);
 string DownloadPath = "C:/Documents/" + fn[2];  //for download in C:/Documents
 if (!File.Exists(DownloadPath))
 {
  FileStream outputStream = new FileStream(DownloadPath, FileMode.Create);
  outputStream.Write(fileeBytes, 0, fileeBytes.Length);
  outputStream.Close();
  return 1;
 }
 else
 {
  return 2;
 }
}

//you need to add following files.
clientaccesspolicy.xml


<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="SOAPAction">
        <domain uri="https://*" />
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true" />
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>


crossdomain.xml


<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type" />
</cross-domain-policy>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900