Read/Write Remote File using WMI C# - c#

I need to read and write(update) some remote machine file.I am able to find the remote file using WMI(System.Management) but not able to do read or updation on that.
Any help would be appreciated.
Thanks
Himanshu

The WMI doesn't have any class (or method) to read or write the content of files. You may only retrieve the metadata (FileName, Date, Size) of the files using CIM_DataFile, or do tasks like Copy, Rename, Delete or Compress files.

RRUZ is correct: WMI cannot copy or create files over a network. This is because it would require credential "hopping":
http://msdn.microsoft.com/en-us/library/windows/desktop/aa389288%28v=vs.85%29.aspx
However, a workaround was recently created by Stackoverflow.com user Frank White in C#, and the WMI logic ports directly to VBS. Here's his solution:
WMI remote process to copy file
I ported it to a fully working VBScript:
https://stackoverflow.com/a/11948096/1569434

First check your file access in premmisions and set user "Everyone" to Full Control
then try it again.

Related

WP8 copy SharedStorge file directly into IsolatedStorage

I am developing a Windows Phone 8 application but am having a lot of issues with file access permission exceptions hindering the approval of my application when ever I try accessing files in the "local" folder (this only happens after the application has been signed by the WP store, not when deployed from Visual Studio). To solve this I have moved all file operations to IsolatedStorage and this seems to have fixed the problems.
I only have one problem left though. My application needs to make use of the file extension system to open external files and this seems to involve the file first being copied to the local folder where after I can then manually copy it into IsolatedStorage. I have no problem in implementing this but it seems that a file access permission exception also occurs once the system tries to copy the external file into the local folder.
The only way I think this can be solved is if I can direct the system to directly copy into IsolatedStorage but I cannot figure how to do this or if it is even possible. It seems as if though the SharedStorageAccessManager can only copy into a StorageFolder instance but I have no idea how to create one that is directed into IsolatedStorage, any ideas?
PS. Do you think that the Microsoft system might be signing my application with some incompetent certificate or something because there is not a hint of trouble when I deploy the application from Visual Studio, it only happens when Microsoft tests it or when I install it from the store using the Beta submission method.
Below is a screenshot of the catched exception being displayed in a messagebox upon trying to open a file from an email:
EDIT:
Just to make it even clearer, I do NOT need assistance in figuring out the normal practice of using a deep link uri to copy an external file into my application directory. I need help in either copying it directly into isolatedstorage or resolving the file access exception.
Listening for a file launch
When your app is launched to handle a particular file type, a deep link URI is used to take the user to your app. Within the URI, the FileTypeAssociation string designates that the source of the URI is a file association and the fileToken parameter contains the file token.
For example, the following code shows a deep link URI from a file association.
/FileTypeAssociation?fileToken=89819279-4fe0-4531-9f57-d633f0949a19
Upon launch, map the incoming deep link URI to an app page that can handle the file
// Get the file token from the URI
// (This is easiest done from a UriMapper that you implement based on UriMapperBase)
// ...
// Get the file name.
string incomingFileName = SharedStorageAccessManager.GetSharedFileName(fileID);
// You will then use the file name you got to copy it into your local folder with
// See: http://msdn.microsoft.com/en-us/library/windowsphone/develop/windows.phone.storage.sharedaccess.sharedstorageaccessmanager.copysharedfileasync(v=vs.105).aspx
SharedStorageAccessManager.CopySharedFileAsync(...)
I've inline the information on how to do this from MSDN http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206987(v=vs.105).aspx
Read that documentation and it should be clear how to use the APIs as well as how to setup your URI mapper.
Good luck :)
Ok I figured it out. The "install" directory is actually restricted access but for some reason the Visual Studio signing process leaves the app with enough permissions to access this folder. The correct procedure of determining a relative directory is not to use "Directory.GetCurrentDirectory()" but rather to use "ApplicationData.Current.LocalFolder". Hope this helps!

'Access denied' to an xml file in windows application

In my .net windows application am using a xml file .
But after installing the application by creating setup, while I am running that 'Access Denied' to that xml file message is shown.
I am using System.IO.File methods for doing file operations.
If any body knows the solution pls share.
Write access to program directory has been more and more restricted (starting with XP/Vista) since that is a secruity risk!
I would suggest to have the "base version" of that file readonly in your program directory...
Upon start of your app you check whether it is present in ApplicationData or LocalApplicationData or CommonApplicationData - (to get the real path at runtime use Environment.GetFolderPath). If it is not there then copy it there - in those locations your application has write access by default with no need for Administrator rights.
Program Files folder has limited access and can be modified by Administrator account. I would suggest you to save your xml file in *C:\Users\YourPCName\AppData\Local\YourAppFolder* .
This way you will be able to access and modify the file
I had this problem once so I used Isolated Storage and that fixed my problem. It may or may not work for you depending on the nature of your situation, but here's a quick tutorial on how to use it:
http://www.codeproject.com/KB/dotnet/IsolatedStorage.aspx

How To Do a Server To Server File Transfer without any user interaction?

In my scenario, users are able to upload zip files to a.example.com
I would love to create a "daemon" which in specified time intervals will move-transfer any zip files uploaded by the users from a.example.com to b.example.com
From the info i gathered so far,
The daemon will be an .ashx generic handler.
The daemon will be triggered at the specified time intervals via a plesk cron job
The daemon (thanks to SLaks) will consist of two FtpWebRequest's (One for reading and one for writing).
So the question is how could i implement step 3?
Do i have to read into to a memory() array the whole file and try to write that in b.example.com ?
How could i write the info i read to b.example.com?
Could i perform reading and writing of the file at the same time?
No i am not asking for the full code, i just can figure out, how could i perform reading and writing on the fly, without user interaction.
I mean i could download the file locally from a.example.com and upload it at b.example.com but that is not the point.
Here is another solution:
Let ASP.Net in server A receive the file as a regular file upload and store it in directory XXX
Have a windows service in server A that checks directory XXX for new files.
Let the window service upload the file to server B using HttpWebRequest
Let server B receive the file using a regular ASP.Net file upload page.
Links:
File upload example (ASP.Net): http://msdn.microsoft.com/en-us/library/aa479405.aspx
Building a windows service: http://www.codeproject.com/KB/system/WindowsService.aspx
Uploading files using HttpWebRequest: Upload files with HTTPWebrequest (multipart/form-data)
Problems you gotto solve:
How to determine which files to upload to server B. I would use Directory.GetFiles in a Timer to find new files instead of using a FileSystemWatcher. You need to be able to check if a file have been uploaded previously (delete it, rename it, check DB or whatever suits your needs).
Authentication on server B, so that only you can upload files to it.
To answer your questions - yes you can read and write the files at the same time.
You can open an FTPWebRequest to ServerA and a FTPWebRequest to ServerB. On the FTPWebRequest to serverA you would request the file, and get the ResponseStream. Once you have the ResponseStream, you would read a chunk of bytes at a time, and write that chunck of bytes to the serverB RequestStream.
The only memory you would be using would be the byte[] buffer in your read/write loop. Just keep in mind though that the underlying implementation of FTPWebRequest will download the complete FTP file before returning the response stream.
Similarly, you cannot send your FTPWebRequest to upload the new file until all bytes have been written. In effect, the operations will happen synchronously. You will call GetResponse which won't return until the full file is available, and only then can you 'upload' the new file.
References:
FTPWebRequest
Something you have to take into consideration is that a long running web requests (your .ashx generic handler) may be killed when the AppDomain refreshes. Therefore you have to implement some sort of atomic transaction logic in your code, and you should handle sudden disconnects and incomplete FTP transfers if you go that way.
Did you have a look at Windows Azure before? This cloud platform supports distributed file system, and has built-in atomic transactions. Plus it scales nicely, should your service grow fast.
I would make it pretty simple. The client program uploads the file to server A. This can be done very easily in C# with an FtpWebRequest.
http://msdn.microsoft.com/en-us/library/ms229715.aspx
I would then have a service on server A that monitors the directory where files are uploaded. When a file is uploaded to that directory or on certain intervals it simply copies files over to server B. Again this can be done via Ftp or other means if they're on the same network.
you need some listener on the target domain, ftp server running there, and on the client side you will use System.Net.WebClient and UploadFile or UploadFileAsync to send the file. is that what you are asking?
It sounds like you don't really need a webservice or handler. All you need is a program that will, at regular intervals, open up an FTP connection to the other server and move the files. This can be done by any .NET program with the System.WebClient library, doesn't have to be a "web app". This other program could be a service, which could handle its own timing, or a simple app run by your cron job. If you need this to go two ways, for instance if the two servers are mirrors, you simply have the same app on the second box doing the same thing to upload files over to the first.
If both machines are in the same domain, couldn't you just do file replication at the OS level?
DFS
set up keys if you are using linux based systems:
http://compdottech.blogspot.com/2007/10/unix-login-without-password-setting.html
Once you have the keys working, you can copy the file from system A to system B by writing regular shell scripts that would not need any user interactions.

C# FTP File Integrity Check

how do we know if the file downloaded from FTP is not corrupt? The FTP server does not provide any checksums of their files or is there a checksum command? Any other ways of checking downloaded file's integrity? Thanks.
Other than checking to see if the file will actually open in the target application or if it's missing an EOF marker or something like that, it's going to be tough to validate the contents of a downloaded file without some image or checksum to compare it to.
Perhaps, using an MD5 File Verification Service (custom) of course would enable you to schedule a service in a selected repository to verify files to their md5 signatures...
Generally, this is done on the download rather than on uploads...but, ya know you may be able to figure out a way to do this for uploads.

IIS Permissions for Saving Word Documents

Our ASP.NET/C# lets users edit and manage Word (OpenXML) documents that are hosted on a server. I am using client-side VBScript functions to handle some of the editing functions including saving the document to a folder on the server. For the save functionality, I am using the following function call :
Document.SaveAs "http://server/savefolder/savefile.docx"
I have given "Full Control" permissions on savefolder to both the NETWORK SERVICE and the IUSR_MACHINE users. Yet the above call fails. The error number returned is 5096. The error message is some gibberish that doesn't make any sense.
The server is Windows 2003 and the IIS version is 6.0. I have installed the OpenXML SDK 2.0 CTP on the server.
I can successfully read and print documents.
Does anyone tell me what I am doing wrong? or what additional settings need to be in place?
BTW, the error message ("gibberish" from my post) is:
"EOALPHABETICARABICARABICABJADARABICALPHABAHTTEXTCAPSCARDTEXTCHARFORMATCHI"
No, I am not making this up!
In my case, that error 5096 with description "EOALPHABETICARABICARABICABJADARABICALPHABAHTTEXTCAPSCARDTEXTCHARFORMATCHI"
occurred when using VBA code in Access to drive a Word mail-merge. The cause was trying to save a document with the same name (including path) as an open document.
Error line:
objApp.ActiveDocument.SaveAs saveAsName
where objApp is the object variable representing the Word application and saveAsName is the string variable storing the name I am trying to save the file as e.g. "C:\temp\testdoc.docx".
IF a file with the same name exists but is not open, the above code overwrites it silently.
Turns out WebDAV is not turned on by default in IIS 6.0. Once I turned it on, I was able to save the documents just fine.
Thanks for all your answers!
Just a guess... if the vbscript is running on the client, the code is probably running under the user's account, not under the server's IIS account. So unless you give write access to that user, vbscript probably won't work for this.
Since you're using ASP.NET, you could try writing a web service that takes in Word document data and saves it to the server for you.
I'd try running Fiddler on the client while trying to save the document to get a sense of what's really going on. I wonder if maybe it's trying to do an HTTP PUT (as opposed to a POST).
Have you given write access to the folder in IIS manager?
Is the save folder you're using outside of the websites root directory, i.e. 'hidden' from the internet?
Just to add to SI's information...
I also get this I get the 5096 - EOALPHABETICARABICARABICABJADARABICALPHABAHTTEXTCAPSCARDTEXTCHARFORMATCHI error when my code tries to save a MS Word document with the same name and to the same location as a Word document that is already open in another instance of Word.
Although not entirely relevant to this thread, I hope it may help someone else who stumbles upon this thread!
Regards,
Duane,
this question is old but still active ?
You save the file with a http://... url, i think you should save it with a file URL as
Document.SaveAs "\\server\savefolder\savefile.docx"
Grtz

Categories

Resources