Ask Question Forum:
Model Library:2025-02-08:A.I. model is online auto reply
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by countrymeister
at 2024-07-08 04:21:42
Point:500 Replies:5 POST_ID:828938USER_ID:11836
Topic:
C# Programming Language;;.NET
I am copy a pdf file from Location A to Location B. After copying I want to open the file.
This gives me an error stating access to the file is denied.
private void OpenFile(string fileDirectory , string fileName)
{
try
{
string currFile = String.Format("{0}{1}", fileDirectory, fileName);
string path = System.IO.Directory.GetCurrentDirectory();
string newFile = String.Format("{0}{1}", path, fileName);
File.Copy(currFile, newFile, true);
FileAttributes currAttributes = File.GetAttributes(newFile);
File.SetAttributes(newFile, currAttributes | FileAttributes.ReadOnly);
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = newFile;
process.Start();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + "" + ex.StackTrace);
}
}
This gives me an error stating access to the file is denied.
private void OpenFile(string fileDirectory , string fileName)
{
try
{
string currFile = String.Format("{0}{1}", fileDirectory, fileName);
string path = System.IO.Directory.GetCurrentDirectory();
string newFile = String.Format("{0}{1}", path, fileName);
File.Copy(currFile, newFile, true);
FileAttributes currAttributes = File.GetAttributes(newFile);
File.SetAttributes(newFile, currAttributes | FileAttributes.ReadOnly);
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = newFile;
process.Start();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + "" + ex.StackTrace);
}
}
Author: countrymeister replied at 2024-07-08 07:53:24
DunCan and Kimputer
Thanks for your suggestions, I was able to resolve my issue by giving the newfile a new name which inlcudes the local utc time plus the userid of the user, since the file needs to be accessed by different users from the original location.
So the steps are,
1) take the file from the original location,
2) rename id with local utc time plus username
3) and then open the file.
private void OpenFile(string fileDirectory , string fileName)
{
try
{
string fileExtension = Path.GetExtension(fileName);
string currFile = String.Format("{0}\{1}", fileDirectory, fileName);
string path = System.IO.Directory.GetCurrentDirectory();
string newFile = String.Format("{0}\{1}_{2}_{3}{4}", path, fileName.Replace(fileExtension, string.Empty), DateTime.Now.ToFileTimeUtc().ToString(), Environment.UserName,fileExtension);
File.Copy(currFile, newFile, true);
FileAttributes currAttributes = File.GetAttributes(newFile);
File.SetAttributes(newFile, currAttributes | FileAttributes.ReadOnly);
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = newFile;
process.Start();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}
}
Thanks for your suggestions, I was able to resolve my issue by giving the newfile a new name which inlcudes the local utc time plus the userid of the user, since the file needs to be accessed by different users from the original location.
So the steps are,
1) take the file from the original location,
2) rename id with local utc time plus username
3) and then open the file.
private void OpenFile(string fileDirectory , string fileName)
{
try
{
string fileExtension = Path.GetExtension(fileName);
string currFile = String.Format("{0}\{1}", fileDirectory, fileName);
string path = System.IO.Directory.GetCurrentDirectory();
string newFile = String.Format("{0}\{1}_{2}_{3}{4}", path, fileName.Replace(fileExtension, string.Empty), DateTime.Now.ToFileTimeUtc().ToString(), Environment.UserName,fileExtension);
File.Copy(currFile, newFile, true);
FileAttributes currAttributes = File.GetAttributes(newFile);
File.SetAttributes(newFile, currAttributes | FileAttributes.ReadOnly);
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
process.StartInfo = startInfo;
startInfo.FileName = newFile;
process.Start();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}
}
Expert: Kimputer replied at 2024-07-08 05:18:29
I tested the code. I only get an error on the second run, which is only logical (since the destination file is readonly, set by you, therefore the copy overwrite code is useless in the File.Copy line). Are you sure that if you start totally new (no files at destination folder) it doesn't even run the first time?
I just see you solved it with admin role. However, the copy overwrite error will still happen. File.Copy should be used AFTER extra file.exist and/or delete code (because you use the set readonly flag in your code)
I just see you solved it with admin role. However, the copy overwrite error will still happen. File.Copy should be used AFTER extra file.exist and/or delete code (because you use the set readonly flag in your code)
Expert: duncanb7 replied at 2024-07-08 05:10:34
Could you give the file permission first to other users ?
Please take a look for the reference
http://social.msdn.microsoft.com/Forums/en-US/7ae13690-62f7-447d-a738-983f07b18686/how-to-change-the-permissions-of-a-file-on-the-disk-thru-code?forum=csharplanguage
Duncan
Please take a look for the reference
http://social.msdn.microsoft.com/Forums/en-US/7ae13690-62f7-447d-a738-983f07b18686/how-to-change-the-permissions-of-a-file-on-the-disk-thru-code?forum=csharplanguage
Duncan
Author: countrymeister replied at 2024-07-08 04:54:23
Yes, I can open it directly from Adobe software at Location B, so I don't need the unlocker.
It does copy the file.
Realized that this has to do with Administrative rights. Running Visual Studio as an Adminstrator I was able to run the application and open the file
It does copy the file.
Realized that this has to do with Administrative rights. Running Visual Studio as an Adminstrator I was able to run the application and open the file
Expert: duncanb7 replied at 2024-07-08 04:42:14
Is the file at location A copying to location B? (check copy coding is okay or not)
Could your check it existing at location B first
after running program ?
Could you open the copied pdf file at location B directly through Adobe Software ?
Is the file at original location A unlocked ?
Try to download unlocker software to unlock the file first if it is locked
http://filehippo.com/download_unlocker
Hope understand your question completely. If not please point it out
Duncan
Could your check it existing at location B first
after running program ?
Could you open the copied pdf file at location B directly through Adobe Software ?
Is the file at original location A unlocked ?
Try to download unlocker software to unlock the file first if it is locked
http://filehippo.com/download_unlocker
Hope understand your question completely. If not please point it out
Duncan