I have an app with which at startup it downloads a file from a remote location (through the net) and parses it's contents.
I am trying to speed up the process of startup as the bigger the file gets the slower the app starts.
As a way to speed up the process I thought of getting the last modified date of the file and if it is newer from the file on the user's pc then and only then download it.
I have found many ways to do it online but none of them are in C# (for windows store apps). Does anybody here know of a way of doing this without the need to download the file? If I am to download the file then the process is sped up at all.
My C# code for downloading the file currently is this
const string fileLocation = "link to dropbox";
var uri = new Uri(fileLocation);
var downloader = new BackgroundDownloader();
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("feedlist.txt",CreationCollisionOption.ReplaceExisting);
DownloadOperation download = downloader.CreateDownload(uri, file);
await download.StartAsync();
If it helps the file is stored in dropbox but if any of you guys have a suggestion for another free file hosting service I am open to suggestions
Generally, you can check the file time by sending HEAD request and parsing/looking HTTP header response for a Last-Modified filed. The remote server should support it and DropBox does not support this feature for direct links (only via API). But DropBox have another feature, the headers have the etag field. You should store it and check in the next request. If it changed - the file has been changed too. You can use this tool to check the remote file headers.
Related
I want to save a file from a http link to the local drive just temporarily in order to access it, this one is working so far and I'm getting the data but a need to write this data to a local file, for example to C:\Windows\temp\test.text, this file should be deleted afterwards.
WebClient client = new WebClient();
string url = "http://www.example.com/test.text";
var file = client.DownloadData(url);
could any one help me on this, thank you!
You cannot write a file on client machine due to security, Any program executing in the browser executes within the browser sandbox and has access to limited features like printer, cookies, etc.
You can write the data to file as a Response object to the client's browser. The Client has the choice of whether to save it or not to his machine.
i have a question, I have this code
foreach (string line in File.ReadLines(**#"C:\fis32v6\fis32.ini"**))
{
if (line.Contains("TEST1"))
{
Label1.Text="TEST1";
PdLine = "1";
}
}
DataSet ds;
ds = GetData(PdLine.ToString());
I want to read from txt file on client specific line with condition. When developing this and building the code it works, what ever i change in txt file can be read from my PC. But when I run the website on server it reads the txt file on that server instead of client I opened the website.
Is there any possibility to make the path relative?
As John mentioned that would be a huge security issue, mostly to make sure the website doesn't dig around in your system.
However it can be instigated from client side.
Just have search here on SO for 'upload file using asp.net' there are loads of hits with answers listed.
You didn't mention specific versions you use (MVC?, asp.net? / core?), and no context as to what workflow your code is running in (is it run on connection or during a specific process), is it configuration settings used for the web session itself? but it is possible to upload.
Should you require the file during startup that might be a bit trickier as you'd have to upload it somehow.
If however it is settings for the web session, why not save it in a cookie?
I have made a Windows service that;
Downloads a CSV file from a regularly updated internal database through a WebClient every hour.
Put that CSV file into a designated folder.
In the original test case, it was put into a local folder on my desktop (C:).
The test case worked perfectly.
The CSV would replace the old file with the newly downloaded one with the same name.
As listed above. This works perfectly on a local folder. However, we intend for it to work through Google Drive File Stream. As we have a Google Sheet that manipulates and sought the data for us for any CSV file that is under the given name.
This is the current method of downloading and placing the file.
public void CSVDownload()
{
string url = #"YOUR_CSV_URL_HERE";
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(new Uri(url), #"YOUR_GOOGLE_DRIVE_FILE_STREAM_FOLDER_HERE\NAME.csv");
void client_DownloadFileCompleted(object CSVDownload, AsyncCompletedEventArgs e)
{
eventLog1.WriteEntry("CSV File (NAME.csv) downloaded");
}
}
My question is why can't I currently automatically upload a file to Google Drive File Stream through a Windows Service? Is it because Google Drive File Stream requires certain permissions or actions? Is it because the drive is "virtual" and not physical (H:)? Below is the folder I am trying to upload to in Google Drive File Stream (H:\My Drive\Test).
The code also runs through completely as the log files show that the methods are used. However, there seems to be some block between the download and placing of the CSV file in a Google Drive File Stream folder.
Update: There has been little progress so far. One of my colleagues maybe suggests that there needs to be some sought of user permission to push. Like a username and password. If this is true does anyone know how I could achieve this?
Update #2: In the 'Registry Editor' I found some interesting info.
Press Windows + R
Type regedit.exe
HKEY_CURRENT_USER -> SOFTWARE -> Google -> DriveFS -> Share
As you can see there are two values. 'MountPoint = H' is obviously the drive letter it is mapped to and 'ShellIpcPath = \\.\Pipe\GoogleDriveFSPipe_user.name_shell' which might be useful. I played around with ShellcpPath but to no prevail.
Update #3: #Ben Voigt mentioned to use DriveInfo.GetDrives() to see if the drive is found by the service. I run the code and it looks like it does exist.
Here is what the console spit out:
Drive H:\
Drive type: Fixed
Volume label: Google Drive File Stream
File system: FAT32
Available space to current user: 15987068928 bytes
Total available space: 15987068928 bytes
Total size of drive: 16106127360 bytes
As you can see it exists however it uses File system: FAT32 instead of File system: NTFS which all my other drives use (C:),(D:),etc. So it seems that only Google Drive File Stream uses FAT32.
Test Case:
Works perfectly when placing the CSV files into a local folder.
client.DownloadFileAsync(new Uri(url), #"C:\Users\user.name\Desktop\Test\designtasks.csv");
client.DownloadFileAsync(new Uri(url), #"C:\Users\user.name\Desktop\Test\designjobs.csv");
Does not work when placing into a Google Drive File Stream folder.
client.DownloadFileAsync(new Uri(url), #"H:\My Drive\Test\designtasks.csv");
client.DownloadFileAsync(new Uri(url), #"H:\My Drive\Test\designjobs.csv");
To clarify about the test case. I am actually getting two CSV files. However, the code is basically the same. So I only mention getting one in my original question to keep it cleaner and more straightforward.
I found the solution! It might not be optimal for all cases however it works in my case.
Here is what I did;
Make a project installer with InstallerProjects.vsiz. Obviously, if you are using a different version of VS, this might change. Some of the older version already come with it installed and the newer versions do not automatically include it. You will have to also find the correlating version as the one linked is for VS2017. Here is a tutorial on how to implement an installer on a windows service.
Once you have implemented the installer, right click on serviceProcessInstaller1 (or whatever you decided to call it) and select properties.
In the properties window, look a for the 'Account' field and set it to 'User'.
Save and build you project. That's it.
What does's this actually do? In plain English; It basically makes the service impersonate a user so it can read and write files on the system.
When installing on a different computer from which the service was built on, 'Windows Defender Shield' will block the service from installing. This is just Windows bring cautious. Obviously, if you built the service without ill intent this will not harm your computer.
To continue installation click "More info" on the 'Windows Defender Shield' screen and click "Run anyway".
Before the Service finishes installing, it will stall and ask for user information. In most use cases, giving it your own user information ill suffice.
If you are a user on a domain or anything along those lines. You will need to add the domain prefix to the 'Username' field. Example: DOMAIN\user.name.
I'm rather new to development and I have a problem which I haven't sound a solution for. I can't seem to find if it is possible or not to solve, anyway..
I want to create an asp page which would allow a user to download a whole folder from an ftp server. The ftp server itself is not on the same physical server as the asp site. To further complicate the issue is that I want to use either explicit or implicit transfer which I can't seem to work in a browser.
The webpage acts as an intermediary between the client and the ftp server, and is meant to be as user friendly as possible. eg. the user just have to press download and it automatically downloads from the ftp server without the use of an installed local client.
client -> asp page -> ftp server
client <- ftp server
My problem is that the asp page does not have the permission to create files on the client system. I have managed to connect to the ftp and try to download all files in a folder but the files do not appear on the client in the target folder.
I might just be searching for the wrong terms but I would appreciate any feedback.
when you say "client" I assume you are referring to the asp.net server. In that case you need to check what user the app pool for your website is running under. Then you need to make sure that user has write permissions to the folder you are storing the ftp files in.
The user is most likely network service.
Your ASP site will not be able to write directly to the end user's system. Just think, would you want any website to have direct access to your file system?
You could download the files to a temporary folder on the Web Server, and then use write it in a response to prompt the user to download it.
Here is a SO question regarding downloading files in ASP.NET
From our question's comment discussion, looks like you are looking to provide user with a option to download file which you have collected on server side from ftp server
//Connect to your file
FileStream sourceFile = new FileStream(Server.MapPath(#"FileName"), FileMode.Open);
float FileSize= sourceFile.Length;
//Write the file content to a byte Array
byte[] getContent = new byte[(int)FileSize];
sourceFile.Read(getContent, 0, (int)sourceFile.Length);
sourceFile.Close();
//Build HTTP Response object
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Length", getContent.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.BinaryWrite(getContent);
Response.Flush();
Response.End();
Please see if the above code helps.
Also, check out HTTP Response file Attachment Discussion
I'm trying to download a file using the Tamir SSH library. I'm able to connect to the remote FTP site, upload files to the site, but I'm getting exceptions when trying to download. I've given IIS_IUSRS full control of the local directory as well as ASPNET. I've tested an I'm able to create a text file in the same local directory I'm trying to download to. Any ideas?
string SFTP_HOST = ConfigurationManager.AppSettings["AccentivFtpHost"];
string SFTP_USERNAME = ConfigurationManager.AppSettings["AccentivFtpUsername"];
string SFTP_PASSWORD = ConfigurationManager.AppSettings["AccentivFtpPassword"];
Sftp client = new Sftp(SFTP_HOST, SFTP_USERNAME, SFTP_PASSWORD);
client.Connect(22);
client.Get("test.txt", "c:\\test.txt");
You probably lack a '/' character in the file directory. You may need to put it either in the Get function call before the "test.txt" like "/test.txt" or at the end of your AccentivFtpHost value in the app config file.