Word File savings without closing are Not Uploaded to the server - c#

In my case, I want to build my own "drop box" like application which I am going to use as a part of my another project.
Discription:
When a word file is opened in the "drop box" folder(inside the folder where changes to the files, file creations deletions ect.. are identified). pictures, txts, txt updates are uploaded to the server without any issue.
But when it comes to office documents. office document creation is uploaded.
Problem:
when the word file is opened, and do some update and save it. the file can not be uploaded due to permission error. even the opened file can not be copied to another place and then uploaded.
Any one faced this kind of issue, and any sugessions.
But we can manually copy and save a opened and saved(but not closed) to another location
But in the program it is not allowed.

You can create another copy of file, this is important because uploading may be slower and reading shared file may lead to conflicts for Word, so what you can do is, you can create a copy quickly on temp file and upload the temp file.
string tmp = Path.GetTempFileName();
using(Stream s = new FileStream(filePath,
FileMode.Open, FileAccess.Read,
// following option will let you open
// opened file by other process
FileShare.ReadWrite)){
using(FileStream fs = File.OpenWrite(tmp)){
// this will copy file to tmp
s.CopyTo(fs);
}
}
// upload tmp file...

your problem is similar to what we faced. In our case we are all connected to a domain directory and the problem was the antivirus installed on our server gives read/write permissions to users (executing exe, installing apps). so you specifically need to give a user the right to execute an app that wants to use another app, in this case office docs.
The problem extended to asp apps using Crystal Reports. hope it helps.

Related

How to prevent locking of a shared file while it is being read/written in C#?

I have a desktop application and below is the flow that is to be followed.
During app initialization, an API should be hit and an excel should downloaded to a shared location. After download is complete the app should read the excel file. This won't be a problem with a single instance of app running. But since this is a desktop app, multiple instances (on different computers) are run, app every time during initialization, downloads the file. I'm using OLE Db engine to read the file and the file is being locked and there 's error "The ole db engine cannot read the file because it is opened by another user " while another instance of the app is opened. How to prevent this?
if (response.Result.IsSuccessStatusCode)
{
using (Stream streamToWriteTo = new FileStream(pathToDownloadReport, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
response.Result.Content.CopyToAsync(streamToWriteTo).Wait();
}
}
If you want to have concurrent access to a file you need to make sure every client only takes a read-lock on the file. With OleDb you can open a connection to the file with ReadOnly access with a connection.
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\path\to\your\exelfile.xlsx;Extended Properties=\"Excel 12.0;IMEX=1;ReadOnly=true;\""
You still need to make sure no one opens the file in Excel.
Since you only can have read-only access to your file, you might as well make a local copy of the file and open that instead. That way locking won't be a problem.

Opening an xlsx document in WebDav prompts for rename

We have a WebDav server written in C# that allows users to access documents. Normally, when a document is opened via file explorer, the user is able to make changes and save the document back with no issues. However, if an xlsx document is opened, the user is greeted with the following message:
The file has been renamed. We recommend saving it now to get the new name.
If you attempt to save, it fails saying
You file was renamed, but something went wrong and your recent changes can't be saved.
At this point, the user can use "Save A Copy" to save the file and override the file on the server as if it was a different document.
Since all other documents work fine (including other Office documents) I think the issue is with Excel rather than our code.

Restrict Access To Certain File From Other Programs And Users While Running

I am in process of building an app which writes data continuously to a file. When I start the program the file is created and starts being written to.
However I noticed that sometimes if I have Windows Explorer open, access to the file is denied to my app, and an error is thrown.
fs = new System.IO.FileStream(location,
System.IO.FileMode.Open,
System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);
So how do I restrict access to this file so only my app can access it, not any other programs?
You can change the last parameter from System.IO.FileShare.ReadWrite to System.IO.FileShare.None.
That locks the file in location exclusively as long as this stream is open and no other app can read, modify or delete that file except your own FileStream.
In fact this isn't only true for other apps - even your own app can't open another FileStream of that file. So keep it open as long as you need but don't forget to correctly dispose the FileStream after use.

Opening a File in onedrive from two different computers

I have an app that acceses files in one drive.I can easily create/read the file from the computer that i developed the app. As soon as I test the app in another computer I get this error The file cannot be accessed by the system. Here is the code I'm using:
Stream stream = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.Read);
The interesting part is I can see the file is there by calling on File.Exists(fPath) but I still cannot open it.
I appreciate any help. Thanks
I finally managed to fix it. The only thing i have to do is right click on the folder/files in OneDrive and choose "Make available offline"

How can I include saving in a text file in executable file?

I have an application in Visual Studio C# which includes saving into a text file, how can I have a .exe sent to another computer and not have an exception in saving?
I need to send a .exe file by email (Yes it's possible) and this application includes saving the state of the game. How can I send this .exe file and let the user be able to save in his computer please?
The problem is that when I send my application's executable file on another computer, I'm getting an exception in saving. Because on the other computer I don't have the text file which I'm saving the game.
I am saving here :
StreamWriter myFile = File.CreateText(Directory.GetCurrentDirectory()+"//ConnectFour.txt");
in the obj/Debug/ of the project..
Thanks for your help :)
Sending an executable should work just fine.
Make sure the other computer has the appropriate Microsoft .NET Framework installed.
Latest framework installer: MSDN
Also, make sure the path inwhich you're saving the file to exists on the remote computer. For example, if you're trying to save to the D:\ drive and it doesn't exist. You will get an exception.
Most likely current location is not writable by current user.
Using "current directory" is dangerous as you have no control over where application is launched from. It is very useful for command line utilities, but not so much for regular windowed applications. Use location that you know you can save files to - i.e. "My Documents".
var filePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\",
ConnectFour.txt");
using(var myFile = File.CreateText(filePAth))
{
// save data here.
}
The problem when sending executables by email are the anti-virus-scanners. Some of them refuse e-mails containing executables. Others accept the mails but delete the attachment.
The solution consists in hiding the executable. This can be done by zipping the executable and sending the zip-file.
Often this is enough to solve the problem but some anti-virus-scanners are very smart and even recognize executables within the zip-attachment. The solution here is to encrypt the zip-file with a password. I often just use the password "pwd" and mention it in the e-mail text. The anti-viruses are not (yet) smart enough to understand this text.
UPDATE
Now I understand your problem. It has nothing to do with sending the executable.
An application can determine from which directory it has been started like this
string dir = System.IO.Path.GetDirectoryName(
System.Windows.Forms.Application.ExecutablePath
);
An alternative is (if you don't have a reference to WinForms):
string dir = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly().Location
);
You probably do not have sufficient privileges to save the file on the remote machine. If you give us more information on the exact exception that is being thrown (type of exception, message, stack trace, etc) you will get a more accurate answer.

Categories

Resources